开发者

Bookmarklet to open a new window forwards current window to [Object Window]?

I am using a small bookmarklet whi开发者_JAVA技巧ch opens a webpage in a new window. It works properly on chrome.

However, when I use the same in Firefox it opens a new window with new web page but the page on which this bookmarklet was clicked is forwarded to some page with text [object Window]. How do I solve this issue?

My code:

<a href="javascript:open('http://www.google.com','targetname','height=500,width=500');">Bookmarklet</a>

Please let me know how to solve this issue.

Thanks


You have to "eat" last return value in the JavaScript URL, returning anything typeof returnValue != 'undefined' will be equivalent to invoking document.write(returnValue). And window.open returns newly created window object, hence the output of "[object Window]". Surely, you can do that by mindlessly appending void(0) statement, but it is SO clumsy. No-magic version (return value eaten, calling window left undisturbed):

javascript:void(open('http://www.google.com','targetname','height=500,width=500'))

You are likely will expand your bookmarklet, so to prevent cluttering global scope, you'd better go anonymous function way (note the absence of return statement):

javascript:(function(){open('http://www.google.com','targetname','height=500,width=500');/* more code to go */})()


Try this code, I have added "void(0);" to stop the parent window go away after clicked.

<a href="javascript:open('http://www.google.com','targetname','height=500,width=500');void(0);" >Bookmarklet </a>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜