Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 578
Lalith Avatar asked Mar 17 '11 04:03

Lalith


People also ask

What is the bookmarklet button?

Bookmarklets are browser bookmarks that execute JavaScript instead of opening a webpage. They're also known as bookmark applets, favlets, or JavaScript bookmarks. Bookmarklets are natively available in all major browsers, including Mozilla Firefox and Chromium-based browsers like Chrome or Brave.

How do you use a bookmarklet?

Just click the bookmarklet and your browser will run it on the current page. If you don't have a bookmarks toolbar — such as on Safari on an iPad or another mobile browser — just open your browser's bookmarks pane and tap or click the bookmark.


2 Answers

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 */})()
like image 119
Free Consulting Avatar answered Oct 26 '22 10:10

Free Consulting


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>
like image 38
ahgood Avatar answered Oct 26 '22 09:10

ahgood