开发者

jQuery script returns new window on Firefox and Internet Explorer

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..开发者_如何学C]. 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.


return false from your click event handler, that should help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜