I am trying figure out how to refresh page in Safari (5.1) using javascript and nothing seems to work.
So far, I have tried,
window.location.href = window.location.href
window.location = window.location.href
window.location.reload(true)
window.location.replace(window.location.href)
What is the right way of handling page refresh in Safari?
Apparently, Safari on Mac or iOS has a bug with location.reload, so, I've developed this simple cross browser solution taking advantage of the url query string:
function refresh() {
var url = location.origin;
var pathname = location.pathname;
var hash = location.hash;
location = url + pathname + '?application_refresh=' + (Math.random() * 100000) + hash;
}
location.reload(true); // works for safari
If you didn't know already this site, let have a look on it, you will have a lot of example for refreshing page: http://www.quackit.com/javascript/javascript_refresh_page.cfm
You should always use the reload() method from the location object...
window.location.reload();
Set the first argument to true if you want to hard reload (send a new GET request for the page).
Related
I have been trying to figure this out all afternoon, but have given up and now turning to you clever people to help :)
I have the following Jquery/Javascript function, which is working fine in Chrome - BUT in IE nothing happens?
$(".btnsubmitpost").click(function () {
var topicid = $(this).attr('rel');
var sbody = tinyMCE.get('txtPost').getContent();
$('.topicpostlistnewpost').remove();
$('.postsuccess').show();
$.post("/myurl/" + topicid + ".aspx",
{ "postcontent": sbody },
function (data) {
var returnUrl = $("value", data).text();
window.location.href = returnUrl;
return false;
});
return false;
});
I have tried window.location, window.location.href both with full urls and absolute Urls but IE just doesn't like it? Any ideas?
The function just gets a Url back from a post, and is supposed to redirect the user to the Url. But like I say, works in Chrome fine just not in IE (Tried IE8 and IE9)
Just for anyone having the same issue, the problem was because the window.location was inside the Ajax post method.
Just replace the window.location with a function() that then calls the window.location or do it after the Ajax call completely.
I am trying to redirect to a different page in IE9 (9.0.3).
When I try to get/set document.location, or document.location.href, or window.location/window.location.href, I'm unable to do so. It fails without giving any errors.
I've tried to check whether the document and windows objects are set, and they are, so I have no idea why the location object is "missing".
I tried getting the document.URL and that works fine, but it's read-only.
Anyone know what the problem is or how to achieve this in a cross-browser way?
I was also experiencing the same problem but found that adding
window.event.returnValue = false;
above line in the javascript before the redirection resolved the problem.
See this: http://social.msdn.microsoft.com/Forums/en/iewebdevelopment/thread/c864ae63-66f6-4656-bcae-86b0018d70c9
Apparently it's a caching bug, you can solve it by appending a timestamp to the destination URL (that is, using a "unique" URL every time).
Perhaps your IE9 has some security restrictions in place that prevent JavaScript from directing URL's. window.location.href = "" should work normally on IE9.
Cache may be the reason, try:
location.href='something.php?tmp=' + Date.parse(new Date())
Hope it helps
You should use an absolute URL:
var url = '/section/page/';
var host = window.location.hostname;
window.location = 'http://' + host + url;
Where url is the relative path to your page.
I'm just building a simple ajax site but running into a problem in safari and chrome. I'm just creating a simple redirect if the user goes to a page i.e.
"/blog"
they would be redirected to
"/#/blog"
code :
url = window.location.pathname
if(url != "/") {
window.location.pathname = "/#" + url
}
This is working great in FireFox but unfortunatly webkit browsers are turning the "#" into a "%23" and giving a 404, for example:
"/%23/blog"
How can I prevent this?
Thanks,
Alex
pathname refers to everything after the host, and before the query string and hash. Consider this instead:
window.location.hash = window.location.pathname;
window.location.pathname = "/";
I'm not exactly sure which browser(s) are implementing the JavaScript spec correctly, but WebKit's behavior seems correct to me.
You are setting pathname, which, by definition, does not include the hash. Webkit is trying to fix that for you (Firefox just made a better guess of what you wanted). Try this:
window.location = '/#/blog';
I have a small problem. For some weird reason any attempt to change url via javascript, be it window.open, window.location, window.location.href, etc. doesnt move to the desired page, but adds it to end of the url. It doesnt even matter what IE version, from 6-8
E.g.
http://localhost/blabla/produkt/philips-fc-861501-animal-care/3639
ends in
http://localhost/blabla/produkt/philips-fc-861501-animal-care/added-by-javascript
I have no idea why this happens...
On this page
http://localhost/blabla/objednat-tovar?step=deal-detail
it works as intended.
Any help is appreciated...
EDIT:
Some code.
I am on
http://localhost/blabla/produkt/philips-fc-861501-animal-care/3639
// code
test
function aaa(where) {
window.location = where;
}
Ends in
http://localhost/blabla/produkt/philips-fc-861501-animal-care/new_location
Same thing happens with window.location.href, window.open and only in IEs
I'm guessing that the browser tries to parse the location as a URL and if it fails then, presumably, it does whatever it wants (IE seems to append the string to the current location). For example:
window.location = 'about:blank'; // OK, since it's a valid pseudo-url.
window.location = 'foo'; // No effect, since this isn't a URL.
window.location = 'http://example.com/'; // OK, browse to that page.
window.location = 'bar'; // Depends on what the browser wants to do...
you don't need javascript: here
test
function aaa(where) {
window.location = where;
}
I would like to have the History.back(); functionality with a complete refresh of previous page.
Any idea how to do that (and make it work in IE, FF and Chrome).
You could redirect (by window.location) to document.referrer
i.e.
window.location.href = document.referrer;
Internet Explorer fix for passing referrer to a particular location:
if(IE){ //IE, bool var, has to be defined
var newlocation = document.createElement('a');
newlocation.href = URLtoCall;
document.body.appendChild(newlocation);
newlocation.click();
}
You can also use the location replace() method:
window.location.replace(document.referrer)