How to remove one part on window.location in jquery - javascript

I want to check window.location, if have "IsReload=True" keyword, I'll remove it. For example: this my window.location:
http://localhost/Demo/User/ManagedUsersTab?isReload=True
I want to remove "?isReload=True" and no reload this page, just remove it. How can I do it. This my snippet:
if (window.location.href.indexOf("?isReload=True") > -1) {
window.location.replace('#Url.Action("ManagedUsersTab", "User")');
}
this snippet reload page

Try
var regex = /(\?|&)isReload=True/;
var location = window.location.href;
if(regex.test(location)){
window.location =location.replace(regex, '$1')
}

Related

Hiding Content Based on Content of URL String

I am trying to hide a certain fieldset with an id of "retail" based on if the URL has 'studio' as a part of it. The URL would read as follows:
/island-careers/studio/accounting/accountant/apply.html
Here is my script I have written up but I can't seem to get it to recognize if 'studio' is in the URL.
<script>
jQuery(document).ready(function(){
var path = window.location.pathname;
console.log(path);
var split = window.location.href.split('/');
console.log(split);
console.log(split[4]);
if (split[4] === 'studio') {
jQuery('#retail').css('display, none');
}
});
</script>
The "console.log(split[4]); was to find the location of 'studio' in the array. There must be something wrong with my IF statement I guess. I also tried this method but it didn't work for me either:
<script>
jQuery(document).ready(function(){
var path = window.location.pathname;
console.log(path);
if (path.indexOf('studio') >= 0) {
jQuery('#retail').css('display, none')
}
});
</script>
Your jQuery line to change the CSS should be:
jQuery('#retail').css('display', 'none');

Using Javascript to convert ? in URL to anchor

How can I convert this:
http://example.com/?thisurl
to this:
http://example.com/#/thisurl
I have a bunch of old URLs that need to redirect to specific anchors on my page but I can't do it with htaccess because the anchors don't get passed through.
Any thoughts?
Something like this should work:
window.location.href = window.location.href.split('?')[0] + '#/' +
window.location.search.replace(/^?/, '');
You can use the .hash and .search parts of the window.location object like this:
window.location.hash = "/" + window.location.search.substr(1);
This will set the hash without reloading the page.
If you want to reload the page so it has a new URL (without the ?thisUrl in the URL bar, then you could do this:
var wl = window.location;
if (wl.search.length > 1) {
wl.href = wl.href.replace(/\?.*$/, "") + "#" + wl.search.substr(1);
}

Using an if statment with a partial address to adress a link

Trying to figure out a way to use Javascript to set up a little if-else statement using only part of the url to determine if a link should go one place or another. So far what I got is,
<script type="text/javascript">
if (url.indexOf("example.com/") != -1)
{
Blahblah
} else {
Blahblah
}
</script>
The problem is that the link doesn't even appear so I don't know how wrong or not I am.
Thanks for any help.
Edit: Lets just say for the sake of argument it is a blank html page. As in <html>
<body>
</body>
</html>
Looking for more of a proof of concept then branch out into getting this working on a full scale site.
Edit #2:
Figured it out, even has url detection.
<a id="link">link</a>
<script type="text/javascript">
var link = document.getElementById('link');
var referrerUrl = document.referrer;
if (referrerUrl.indexOf("searchurlfor") != -1)
{
link.href = "place1";
} else {
link.href = "place2";
}
</script>
Try not mixing HTML and JavaScript:
<a id="someLink" target="_blank">Blahblah</a>
<script>
document.getElementById('someLink').href =
(url.indexOf("example.com/") != -1)? 'placeone.htm' : 'placetwo.htm'
</script>
or more verbosely:
<script>
var linkElem = document.getElementById('someLink');
if(url.indexOf("example.com/") != -1) {
linkElem.href = 'placeone.htm';
} else {
linkElem.href = 'placetwo.htm';
}
</script>
Preferably the script should go to a separate file. The way you suggest feels like PHP or JSP but JavaScript does not work this way. In the example above you first render empty link and change the href attribute afterwards.
I think you want:
<script>
if (url.indexOf("example.com/") != -1)
{
document.write('Blahblah');
} else {
document.write('Blahblah');
}
</script>
You need getElementById
HTML:
<a id="link">link</a>
Javascript
<script type="text/javascript">
var link = document.getElementById('link');
if (url.indexOf("example.com/") != -1)
{
link.href="placeone.com";
} else {
link.href="placetwo.com";
}
</script>
You'd have to show us where/when this code is executing in your page. You can't just drop HTML into the middle of a piece of javascript like you were doing.
You can call:
document.write('Blahblah');
to insert HTML into the current place in the document if this is an inline script.
If this code is not executing inline in the document, then you should not use document.write() as that will clear your document and start a new one. Instead, you would use DOM manipulation functions to insert this into the appropriate place in the page or to change the href on an existing link. For example to change the href on an existing link when you have this HTML:
<a id="myLink" href="placeone.com" target="_blank">Blahblah</a>
You would use this javascript that must run after the page has been loaded:
var link = document.getElementById("myLink");
(url.indexOf("example.com/") != -1) {
link.href = "placeone.com";
} else {
link.href = "placetwo.com";
}

Search page and URL for keyword

I would like to check the page for the word "testing", also I would like to check the URL. So for example if you're on site.com/tester that's fine, but if you're on site.com/testing javascript would know. Would you do this with document hostname?
Real world example: Pretty much I just want to create a greasemonkey script to search a page to search a page/url for a username "exgirlfriend" and if "exgirlfriend" is found anywhere on the page document.body.innerHTML= ''
How would I do this?
Here's something that searches both the innerHTML and the URL for your word.
init();
function init()
{
searchWord("exgirlfriend");
}
function searchWord(word)
{
var pageResults = document.body.innerHTML.match(word);
var urlResults = window.location.href.match(word);
if(pageResults || urlResults)
{
alert("word found");
}
}
http://jsfiddle.net/vfbzc/

Add a fragment to the URL without causing a redirect?

Is there is a way how to add hash # to my URL without redirect?
window.location.hash = 'something';
That is just plain JavaScript.
Your comment...
Hi, what I really need is to add only the hash... something like this: window.location.hash = '#'; but in this way nothing is added.
Try this...
window.location = '#';
Also, don't forget about the window.location.replace() method.
For straight HTML, with no JavaScript required:
Add '#something' to URL
Or, to take your question more literally, to just add '#' to the URL:
Add '#' to URL
window.location.hash = 'whatever';
Try this
var URL = "scratch.mit.edu/projects";
var mainURL = window.location.pathname;
if (mainURL == URL) {
mainURL += ( mainURL.match( /[\?]/g ) ? '&' : '#' ) + '_bypasssharerestrictions_';
console.log(mainURL)
}

Categories