javascript: will window.location make any warnings? - javascript

If I use window.location = ""; to redirect a user, will any browsers give the user a warning message (since the page is redirecting without the user's consent)?
Should I use window.location = "" or window.location.href = ""?

No you can redirect the window location on your own. The only similarly related thing you can't do automatically is click a link for the user with an event.
You might be thinking of this type of notice:
window.onbeforeunload = function() {
return "Are you sure you want to navigate away?";
}

No. You will not get any warning (like you do if you close the window). And I have never seen any difference in location and location.href, but I use the last :)

AFAIK window.location and window.location.href should be pretty much eqivalent.
It's been a while since I've used it, but I don't remember ever seeing a prompt before leaving the page. Most of the time when I have been prompted it's been because I explicitly put a confirmation in, such as when clicking a link to delete something.

Setting window.location shouldn't cause any issues with redirection. However, if there is a hash, then some browsers may deal with it differently.
From the MDN spec:
Note: The example above works in situations where window.location.hash does not need to be retained. However, in Gecko-based browsers, setting window.location.pathname in this manner will erase any information in window.location.hash, whereas in WebKit (and possibly other browsers), setting the pathname will not alter the the hash. If you need to change pathname but keep the hash as is, use the replace() method instead, which should work consistently across browsers.
There shouldn't be any difference between location and location.href since whenever a property of the location object is modified, a document will be loaded using the URL as if window.location.assign() had been called with the modified URL.
Source: https://developer.mozilla.org/en/DOM/window.location

Related

Checking that popup has redirected back to opener's domain/port/protocol in JavaScript

I do a window.open(popupURL) to make a popup. The popup redirects to some other site which redirects back to my site. I use window.setInterval() to watch for when the popup gets back to my site. But I can't find a way to gracefully observe that condition. It makes sense to me, for instance, to evaluate the expression
popup.document.domain === window.document.domain
but it seems that I'm not allowed to read popup.document, as it gives me an error related to the fact that the opener is using HTTP while the popup is using HTTPS. I thought that maybe I could use popup.location somehow, so as to avoid asking for that sensitive foreign document, but trying to read anything out of popup.location produces a similar error. I'm not even allowed to read popup.location.protocol! Is the foreign site's choice of HTTP versus HTTPS really such a big secret?
Anyway I can get around these problems by swallowing those errors in an empty catch block. Then my domain check above seems to work. But is there a way that doesn't involve the ugly catch block?
How about adding two functions to the window A. A function that will be triggered onload, and that will call another function that will do whatever you want. Not sure how it's gonna work once the popup is redirected from B to A - that is if it will have access to window.opener though...
In pseudo code:
//This code will go in the window A, which is also loaded in a popup after a redirect.
window.onload = function(){
if( window.opener && !window.opener.closed ){//This is to make sure only the popup will call it.
doSomething();
}
}
function doSomething(){ //Do soemthing }

Javascript Url manipulation and back navigation

I am updating the url with something like this:
window.history.pushState(null, "Page title", "/?param=" + myParamValue);
This works fine, but when the user hits the back button, the url gets updated but the page does not reload.
I have an ajax routine that updates the content but if possible I don't want to mess with re-implementing back/forward navigation, and I just want the page to reload in case of the user hitting back/forward browser buttons.
Q: Is there a way to force the page to reload the given url on browser back/forward actions from the user?
Bonus Q: also, what if the browser doesn't support window.history.pushState on older browsers? Shall I surround that code in a try/catch block?
if(typeof window.history.pushState === 'function')
//handle your url rewriting
else
//support for older browsers
As for your question about back-button support, the link provided in the comment provides very nice implementations for the feature
You should take a look at this jQuery plugin, it might be useful for you:
http://www.asual.com/jquery/address/
I found it reading this thread:
https://stackoverflow.com/questions/116446/what-is-the-best-back-button-jquery-plugin
I hope it helps.
About the old browser question, I don't think you have to bother with that because you are using ajax, which requires a modern browser too. The main question is: who will visit your web site? If it's mainly people over 40yo, maybe you should bother...
Just my opinion ;)
Edit: Be careful with IE9, it's not handled in it. thx to nbrooks for the info

Reload a page once on start for IE9 users with JavaScript

I would like a page to force reload once for IE9 users to clear the cache. I've been experimenting with this function:
location.reload();
The question: is it possibly to target people using IE9 and only reload ONCE on load.
Thankful for any help I can get.
/Linus
It would probably be best if you could devise some strategy which allows you NOT to depend on the browser version.
Having said that, here is a link that will allow you to detect IE9.
reference : stackoverflow similar question
from the link to answer taken
Two options:
1- You can use cookies. Set a cookie when logging in and check for
that during load. Then clear it after performing your initialization
so that next load the check will fail.
2- You can use the address, either as a parameter like ?justLoggedIn,
or a value in the hash after #justLoggedIn. Then check for that and
redirect to the same url minus the justLoggedIn part. The advantage of
the # version is that it doesn't actually reload the page, unlike when
using the ? version.
UPDATE:
reference :stackoverflow similar question
I'd say use hash, like this:
window.onload = function() {
if(!window.location.hash) {
window.location = window.location + '#loaded';
window.location.reload();
}
}
UPDATE 2:
i think you should take a look at this question
i dont know about pjax until now so i dont know alot about it
stackoverflow question
look at http://pjax.heroku.com/in the answers

Can i detect and prevent JavaScript-driven redirect from within Firefox extension?

Basically, i want to make Firefox obey "Warn me when web sites try to redirect or reload the page" user preference. Currently, this is really open sesame for any kind of doorway writers etc.
Please find the detailed description of this misbehaviour in related superuser post.
You can use Object.watch() to intercept changes of some properties like window.location:
function onLocationChange(id, oldval, newval)
{
if (confirm("Do you want to navigate to " + newval + "?"))
return newval;
else
return oldval;
}
wnd.watch("location", onLocationChange);
wnd.document.watch("location", onLocationChange);
wnd.location.watch("href", onLocationChange);
You would have to similarly intercept assignments to other window.location properties like host or pathname. The big disadvantage of this approach: there can be only one watcher. If the page installs its own watcher or simply calls wnd.unwatch("location") all your detection will be gone.
How to get to the window before any page JavaScript has a chance to run depends on whether you are using the Add-on SDK or whether you have a classic add-on. If you are using the Add-on SDK then you use the page-mod module with contentScriptWhen parameter set to start. In the code example above you replace wnd by unsafeWindow (the window has to be accessed directly, otherwise it won't work).
In a classic add-on you register an observer for the content-document-global-created notification. Here you also have to access the window directly:
var wnd = XPCNativeWrapper.unwrap(subject);
See documentation on XPCNativeWrapper.unwrap().
A "JavaScript-driven redirect" would be done by using the window.location property. All you would need to do is replace that property's setter function as soon as the page loads.
Here is a John Resig blog post on defining setters and getters for properties. http://ejohn.org/blog/javascript-getters-and-setters/
I wasn't able to override the default window.location setter using client side js, but it may be possible using a more privileged extension environment though.

UIWebVIew Javascript Document.location

I have been unable to get my UIWebView to change location based in javascript. While this may seem a bit redundant, it is necessary because the client's website stores a cookie when document.location is known. However, when I try accessing the next part of his site, it will not open.
Here is the code
[self.webView stringByEvaluatingJavaScriptFromString:#"document.location = 'http://www.nextLink.com'"];
However, this doesn't seem to have done anything. Also the document.write method works!
Thanks!
Have you tried with window.location instead?

Categories

Resources