jQuery脚本在Firefox和Internet Explorer上返回新窗口 [英] jQuery script returns new window on Firefox and Internet Explorer

查看:193
本文介绍了jQuery脚本在Firefox和Internet Explorer上返回新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面上的元素切换按钮,当我点击它,我可以看到脚本运行和切换元素,但是然后我得到一个新的窗口与JS对象返回[我猜..] 。它只是一个空白的窗口与我的HREF的网址,它说 [对象对象]

在铬的罚款,但ie和Firefox做到这一点。
他们这样做的方式是

$ p $ < a href =javascript:jQuery('#thatDude')。肘节();>点击< / A>
< div id =thatDude> blah< / div>



更奇怪的部分是,我也有这个是我的脚本文件$ b $ ()。
$ pre $ j $ j $ ;
});

当我点击时 - 它在任何地方都能正常工作... [是啊我知道我应该改变生活委托=]]



有人知道最新怎么回事?
im使用jquery 1.6 [但是1.4.4也一样..]

非常感谢

发生什么事是脚本在 javascript: URI中的返回值,如果返回值不是void,被视为HTML并被解析。除了没有实现该功能的WebKit(下面提到一个例外)。在你的情况下,我假设jQuery的 toggle()返回一个实际的值,这个值不是未定义的,这个值被当作一个字符串来解析为HTML。



您可以在IE,Firefox和Opera这个简单的测试用例中看到这种行为:

 < a href =javascript:< h1>一些文字< / h1>'>点击我< / a> 

在WebKit浏览器中完全失败,但这在WebKit中起作用,因为它们专用于一个代码路径:

 < iframe src =javascript:< h1>一些文字< / h1>'>< /的iframe> 

如果你想确保脚本的返回值是无效的,把 void (0)在脚本的最后。


i have this toggle button for an element on my page, and when i click it, i can see the script runs and toggles the element, but then i get a new window with the JS object returned [i guess..]. it just a blank window with a url of my href and it says [object Object]
in chrome its fine, but ie and firefox do this. they way i do this is

<a href="javascript:jQuery('#thatDude').toggle();">click</a>
<div id="thatDude">blah</div>

the even stranger part is that i also have this is my script file

jQuery('.someBtn').live('click', function() {
  jQuery('#someForm').toggle();
});

and when i click that - it works fine anywhere... [yeah i know i should change live to delegate =] ]

does anyone know whats going on? im using jquery 1.6 [but 1.4.4 does the same..]

thanks so much

解决方案

What's going on is that the return value of the script in a javascript: URI, if that return value is not void, is treated as HTML and parsed as such. Except in WebKit, which doesn't implement that feature (with one exception mentioned below). In your case, I assume that jQuery's toggle() returns an actual value that's not undefined, and that value is treated as a string to be parsed as HTML.

You can see the behavior in this simple testcase in IE and Firefox and Opera:

<a href="javascript:'<h1>some text</h1>'">Click me</a>

This totally fails in WebKit browsers, though this does work in WebKit because they special-hack one codepath:

<iframe src="javascript:'<h1>some text</h1>'"></iframe>

If you want to make sure your script's return value is void, put void(0) at the end of the script.

这篇关于jQuery脚本在Firefox和Internet Explorer上返回新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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