Skip to content Skip to sidebar Skip to footer

Ie Javascript Redirection

I have a javascript function which redirects user to a diff page. It goes like this... redirect : function(url) { if(!url) return false; alert(url); if (this.b

Solution 1:

This function is a complete waste of time. Assignment to location.href works equally well in all currently extant browsers. this.browserClass.isNN4 is a hint that this code is worrying about stuff that doesn't exist in this century. As if being stinky old browser-sniffing wasn't bad enough. (Anyway even in Netscape, both these assignments worked.)

setTimeout("location.href = '" +url+"'", 0);

Try not to pass strings to setTimeout, it's the same thing as eval with all the same problems (eg. your URL contains an apostrophe... boom). Pass a function, an inline one if necessary (setTimeout(function() { location.href= url; }, 0);).

However what this smells like to me is you're trapping a click or mousedown event on a link, and not cancelling the event (by returning false from the event handler). Consequently the link following default action can occur and may, depending on browser, override the location.href navigation.

Solution 2:

If you are not concern about the Web Browser keeping history (the back and forward buttons), just use the javascript location.replace(url) function else you can always use the location.href attribute.

Source: http://www.roseindia.net/javascript/javascript-location-replace.shtml

The window.location (if I remember correctly) is an object and not a string attribute.

I don't remember if document.location is still valid in IE6 though (I quit IE6 yonkers ago).

Post a Comment for "Ie Javascript Redirection"