最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

internet explorer - IE javascript redirection - Stack Overflow

programmeradmin2浏览0评论

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.browserClass.isW3C) //IE 6.X es here
    {
        window.location.href = url;
    }
    else if(this.browserClass.isIE4)
    {
        window.location.href = url;
    }
    else if (this.browserClass.isNN4)
    {
        window.location = url;
    }
    else
    {
        window.location = url;
    }
    return false;
},

But the problem is that this does not work in IE (internet explorer 6.X). After a short battle I saw that IE was redirecting when I change the code to this -

    if (this.browserClass.isW3C)
        setTimeout("location.href = '" +url+"'", 0);

Problem is solved. But what's going on here? Could someone educate me? or is it just one of those mind numbing idiosyncrasies of IE...

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.browserClass.isW3C) //IE 6.X es here
    {
        window.location.href = url;
    }
    else if(this.browserClass.isIE4)
    {
        window.location.href = url;
    }
    else if (this.browserClass.isNN4)
    {
        window.location = url;
    }
    else
    {
        window.location = url;
    }
    return false;
},

But the problem is that this does not work in IE (internet explorer 6.X). After a short battle I saw that IE was redirecting when I change the code to this -

    if (this.browserClass.isW3C)
        setTimeout("location.href = '" +url+"'", 0);

Problem is solved. But what's going on here? Could someone educate me? or is it just one of those mind numbing idiosyncrasies of IE...

Share Improve this question asked Jan 20, 2010 at 2:55 Srikar AppalarajuSrikar Appalaraju 73.9k55 gold badges219 silver badges265 bronze badges 1
  • 1 you're designing for IE4 and Netscape?? – nickf Commented Jan 20, 2010 at 3:25
Add a ment  | 

2 Answers 2

Reset to default 4

This function is a plete 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.

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/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).

发布评论

评论列表(0)

  1. 暂无评论