javascriptfirefoxgoogle-chromebookmarklet

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


I am using a small bookmarklet which 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


Solution

  • 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 */})()