11

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

Free Consulting
  • 4,300
  • 1
  • 29
  • 50
Lalith
  • 19,396
  • 17
  • 44
  • 54

2 Answers2

20

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 */})()
Free Consulting
  • 4,300
  • 1
  • 29
  • 50
  • Good answer, I like the first solution which has less characters. thanks! – ahgood Mar 24 '11 at 00:28
  • @ahgood, yeah, but it is only applicable to really short bookmarklets, otherwise it leave intermediate variables (eg: loop counter) as window properties – Free Consulting Mar 24 '11 at 00:52
  • does IE still impose character limit for bookmarklet length, BTW? – Free Consulting Mar 24 '11 at 00:52
  • 1
    FYI: http://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 Mar 24 '11 at 01: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 – Free Consulting Mar 24 '11 at 03:35
  • IE seems to be the only browser with a serious limitation in this regard (2,083-char. limit still present in IE 9 and prob. IE11). Firefox can handle > 100,000 chars. Cf. [What is the maximum possible length of a query string?](http://stackoverflow.com/questions/812925/what-is-the-maximum-possible-length-of-a-query-string) and [What is the maximum length of a URL in different browsers?](http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers) – Medlock Perlman Jan 22 '15 at 12:12
  • Actually, even IE's limit seems to have been improved in IE9 (to 5kb, so ca. 5120 chars, specifically for JavaScript URIs). Scroll down to 'JavaScript URIs' (there's no anchor) in the article '[URL Length Limits](http://blogs.msdn.com/b/ieinternals/archive/2014/08/13/url-length-limits-in-internet-explorer.aspx)' from IEInternals. 2,083 characters leaves quite a lot of scope for a bookmarklet anyway... – Medlock Perlman Jan 22 '15 at 12:28
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>
ahgood
  • 1,847
  • 20
  • 19