5

I noticed that my Javascript bookmarklet was failing on certain sites like Google Reader and Google search results pages (and randomly on some non-Google sites). Looking at the console, I could see that, for these pages, clicking the bookmarklet did not append elements to the head/body like it normally did, but created a new document that looked like this:

<html>
  <head></head>
  <body></body>
</html>

Even when I reduced my bookmarklet to javascript:alert(window.location.href); it would create this blank page and run the bookmarklet therein, so that the alert showed about:blank. On most sites, this blank page is not created and the correct location is shown.

Can someone explain this? Are these sites sandboxing externally run code as a safety measure?

UPDATE: I currently can't reproduce this on browsers other than Chrome 17.0.932.0 dev. Please let me know if anyone else can reproduce these results on Chrome or otherwise.

glortho
  • 13,120
  • 8
  • 49
  • 45
  • I cannot reproduce your findings. Which browser is causing trouble? – Rob W Nov 12 '11 at 20:53
  • Amazing. I could've sworn this was happening across browsers but now I can only reproduce it in Chrome dev. Unless anyone else can reproduce in other browsers, consider this issue on hold. – glortho Nov 12 '11 at 21:18
  • Are you testing through bookmarklets, or pastes in the location bar? – Rob W Nov 12 '11 at 21:23
  • I've been testing through bookmarklets. Certainly worth a paste. Running the same code from the console works properly every time so it seems to be a navigation-away issue with the javascript: links. – glortho Nov 13 '11 at 01:02
  • +1 for ghostly web page. – Dave Newton Nov 13 '11 at 01:21
  • Observing this behaviour for GMail pages opened Google Chrome apps. Google Chrome 16.0.912.41 If GMail opened from gmail.com URL - works as expected. Also, 'code' of the bookmarkled works correctly if I run it in Chrome JS console. – KIR Nov 17 '11 at 14:15

3 Answers3

1

You need to ensure that the topmost code, i.e. the one right after javascript: does not return anything.

Usually this is done by wrapping everything in void():

javascript:void(alert(window.location.href));

It's very odd that it breaks with alert() though since the function itself doesn't return anything...

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • Thanks, ThiefMaster. I did try it with void and had the same results. Still getting it very often in Chrome dev. Open it in FF and bookmarklet it = no problem. Have to try more in Chrome stable and report to them. – glortho Nov 13 '11 at 18:04
1

I think this is a bug in Google Chrome, I've filed it to their bug database: https://bugs.webkit.org/show_bug.cgi?id=72606

KIR
  • 5,614
  • 1
  • 31
  • 23
1

If a javascript: url returns a string it will be used to create a new document:

javascript:'foo bar baz';

This can be a tough issue to debug if you don't know to watch out for it. It can crop up if you use a function that returns a string or end your bookmarklet with a line that sets a string value:

javascript: a = prompt('foo bar baz'); b = a;

A simple solution is to use a closure:

javascript:(function(){ var a; a = prompt('foo bar baz'); window.b = a}());

An alternative is to end with void 0;

javascript: a = prompt('foo bar baz'); b = a; void 0;
zzzzBov
  • 174,988
  • 54
  • 320
  • 367