开发者

firefox javascript: urls cause page redirects?

开发者 https://www.devze.com 2023-01-14 04:56 出处:网络
In most browsers if you enter javascript:$(\'element\').css(\'property\', \'value\'); 开发者_如何学Cwill result in the javascript code being run.

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

精彩评论

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