Window.Location Refreshes instead of Redirects - javascript

I have a JQUERY function as follows
this.getURL = function()
{
var name = getName();
alert("Menu.aspx?name"+name);
//window.location = "Menu.aspx?name"+name;
}
When I alert the URL I am attempting to go to, it is correct. However, when I call window.location on that string, the page just refreshes without going anywhere.
I have similar code where I have used window.location and it works. I typed in the url into my browser and it works as well.
At worst (even if the URL was wrong), I was hoping that it would just redirect me to some URL. However, I can't get it to do anything other than refresh the current page.
Also to clarify, the page which calls this function is not Menu.aspx
Thanks in advance.

If you're using a relative path try setting window.location.pathname, otherwise set window.location.href for a full path.
You may also want to try self.location.href

In my experience, it's been difficult to get redirects like this to work right. I've had to use window.location.replace(<url>). If you're just changing an anchor tag, it's even more difficult. You have to do the following to get it to work in all browsers:
window.location.replace(<url>);
window.location=<url>;
window.open(<url>,'_self');
window.location.reload();

Related

window.location not setting to new value correctly

I am currently having issues with a bit of javascript I am running on a website. I am trying to redirect to a new page however instead of setting the redirect url to the page I pass in it seems to concatenate it to the end of the existing url.
For example if I am current on www.stackoverflow.com/questions and want to go to www.stackoverflow.com/questions/ask the url becomes www.stackoverflow.com/questions/www.stackoverflow.com/questions/ask
When I output the variable containing the URL to console it appears correctly
var nextPage = window.location.hostname + data.redirectUrl + "?confirmationtype=success";
console.log(nextPage);
window.location.href = nextPage;
the above output would be www.stackoverflow.com.au/questions/ask but would redirect to www.stackoverflow.com/questions/www.stackoverflow.com/questions/ask
I have done some searching and in many cases it seems to be related to onpost methods on the form, but I cant see how this would be an issue on my page and I am using a polling timer method to trigger these calls so page posts should not be a factor. Also if I try onpost="return false" other pages that use the same layout fail to post :S
Any help would be appreciated.
Url without protocol ("http:") becomes relative Url - so when you try to set window.location it will not perform navigation.
Something similar to:
var nextPage = window.location.hostname + data.redirectUrl + "?confirmationtype=success";
console.log(nextPage);
window.location = "http://" + nextPage;
You can use window.location.protocol instead "http://", you can use window.location or windows.location.href in code above.
Note that your sample shows some strange window.location.href(nextPage); which is function call, but href is not a function.

How to refresh a page in JavaScript without POSTing?

I've seen this question already, but the top answers all suggest window.location.reload. I've just discovered that this will actually do a POST if your page was loaded with a POST.
I reckon I could do window.location.href=window.location.href but that won't work if there's a hashtag in the URL I'm told.
So how can I get the browser to perform a GET on the current page, including query params (with or without the hash)?
You can manually construct your URL:
window.location.href=window.location.origin + window.location.pathname + window.location.hash;
function refresh() {
window.location.href = window.location.pathname + window.location.search;
};
You don't need the origin (it doesn't work in old IE anyway). You should add .search if you want to keep the query params. Don't add .hash because it won't refresh if there is one.

Javascript/jQuery hash URL doesn't execute

I have a hash URL that doesn't execute the javascript/jQuery unless I manually hit the enter key or refresh the page.
$(function($){
var key=$(location).attr('hash');
if (key){
$('#div').find('div.div2').load('sig.php?k='+key)
}
});
This works fine when I type in the URL manually or refresh the page but whenever I use <a href="url#SecReTkEy"> or Javascript/jQuery functions such as:
$(location).attr('href','url#SecReTkEy');
$('a').prop('href','url#SecReTkEy');
window.location.replace('url#SecReTkEy');
window.location.href('url#SecReTkEy');
Can any one tell me why and how to fix this?
`
you should use
location.href = #whatever
You don't need JQuery in order to retrieve the hash. Use the fully qualified reference to window.location.hash (I'm guessing location didn't work as you already have a var named location).
Also, don't forget to URL encode the key using encodeURIComponent. Additionally the $ is not needed if you want to execute this on document ready.
$(function(){
var key = window.location.hash;
if (key){
$('#div').find('div.div2').load('sig.php?k=' + encodeURIComponent(key))
}
});
I have a hash URL that doesn't execute the javascript/jQuery unless I manually hit the enter key or refresh the page.
This was due to it only launching once rather than having an eventBinder on hashchange.

Javascript page refresh to base URL, stripping query string, on browser resize -

I'm using this piece of javascript code to refresh the browser when the user resizes it:
<script type="text/javascript">
$(window).bind('resize', function(e)
{
if (window.RT) clearTimeout(window.RT);
window.RT = setTimeout(function()
{
this.location.reload(true); /* false to get page from cache */
}, 200);
});
</script>
It works fine, except - the page I'm using has a query string with certain values, like:
http://www.example.com/index.php?w=123&h=456
What I'd like is the same functionality but to have the page refresh to the URL without the query string (or at least without its values).
Is that possible?
since location is an object ,you can change the property 'href' to whatever you like , for example you can change the new location and assign it to the href property:
your current location is : http://www.example.com/index.php?w=123&h=456
when you need to refresh the page without the query string, you can do this: window.location.href = "http://www.example.com/index.php"
hope helps,
good luck
Try using window.location = window.location.pathname instead.
If you to redirect the page to root domain, then use window.location.origin. If you want to include the script name too then use window.location.origin and window.location.pathname. So your redirect can be replace with
window.location = window.location.origin + window.location.pathname

how to refresh web page using javascript?

I want to refresh a web page using javascript and, I knew how to do it but I have one problem,
before refresh:
url: http://www.example.com/index.html#introduction
after refreshed:
url: http://www.example.com/index.html
I want to use that hash as parameter in another javascript function after page has refreshed.
So, how can I save that hash after the page is reloaded?
I belive you have implemented refresh using location.reload() method, instead try redirecting to the same page using location.href like below.
location.href = "http://example.com/index.html#introduction"
This will refresh the page and also maintains # in url.
As commented, if u use only static code then u need to use cookies(if also support legacy browsers) else u can use localstorage(for modern browsers) for storing the hashValue.
to reload without hashvalue use the following code
window.location.href = window.location.href.split("#")[0];
before calling the above code u need to save the hashValue.
var hash = location.hash.substr(1);
localStorage["hashValue"] = hash;
retrive the above value later as
var hash = localStorage["hashValue"];
Try
escape(window.location.href)
if you want to add parameters:
for example
escape(window.location.href="parameter="+param);
try this
window.location.href = window.location

Categories

Resources