IE javascript重定向 [英] IE javascript redirection

查看:77
本文介绍了IE javascript重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript函数,可将用户重定向到差异页面.就像这样...

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 comes 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;
},

但是问题是这在IE(Internet Explorer 6.X)中不起作用.经过短暂的战斗,当我将代码更改为此时,我看到IE正在重定向-

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);

问题已解决.但是,这是怎么回事?有人可以教育我吗?还是仅仅是那些令人发麻的IE特质...

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

推荐答案

此功能完全浪费时间.分配给location.href在所有当前存在的浏览器中同样有效. this.browserClass.isNN4暗示此代码正在担心本世纪不存在的东西.好像老旧的浏览器嗅探还不够糟糕. (无论如何,即使在Netscape中,这两个任务都起作用.)

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);

尽量不要将字符串传递给setTimeout,这与eval是一样的事情,但都存在相同的问题(例如,您的URL包含撇号...繁荣).传递一个函数,如有必要,传递一个内联函数(setTimeout(function() { 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);).

但是,这听起来像是我在链接中捕获一个clickmousedown事件,而不是取消该事件(通过从事件处理程序返回false).因此,遵循默认操作的链接可能会发生,并且可能会因浏览器而覆盖location.href导航.

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.

这篇关于IE javascript重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆