开发者

firefox javascript: urls cause page redirects?

In most browsers if you enter javascript:$('element').css('property', 'value');

开发者_如何学C

will result in the javascript code being run.

In firefox this will cause the page to change to a blank white page with [object] [object]

why is this?


Because Firefox shows the return value of that function. Add void(0); after it to avoid a page redirect. Example:

javascript:$('element').css('property', 'value');void(0);

My preffered way to run code in URLs:

javascript:void(function(){ /*code here*/ })();

In this way, you don't mess with the global namespace:

javascript:var y=1;alert(y);void(0);

Here, window.y contains now 1, whereas window.y is undefined below:

javascript:void(function(){var y=1;alert)y)})();


Just add:

void(0);

at the end.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜