12

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

2 Answers 2

21

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 */})()
7
  • Good answer, I like the first solution which has less characters. thanks!
    – ahgood
    Commented Mar 24, 2011 at 0:28
  • @ahgood, yeah, but it is only applicable to really short bookmarklets, otherwise it leave intermediate variables (eg: loop counter) as window properties Commented Mar 24, 2011 at 0:52
  • does IE still impose character limit for bookmarklet length, BTW? Commented Mar 24, 2011 at 0:52
  • 1
    FYI: subsimple.com/bookmarklets/rules.asp Browser Max chars Netscape > 2000 Firefox > 2000 Opera > 2000 IE 4 2084 IE 5 2084 IE 6 508 IE 6 SP 2 488 IE 7 beta 2 2084
    – ahgood
    Commented Mar 24, 2011 at 1:45
  • 1
    @ahgood, thanks, good to know IE users no longer so constrained. just checked - modern Opera is able to store and execute bookmarklets of considerable size of 100 000 chars Commented Mar 24, 2011 at 3:35
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.