0

I am trying to run some JS code in a bookmarklet in firefox. Here's some basic code to prove the point:

window.setTimeout( function() {alert('i ran');}, 1000 );

when I run code with setTimeout in it, I get the whole page replaced by the counter value that normally gets logged in the console.

Is there a way to catch this output and stop this happening?

Thanks!

2 Answers2

5

Try the following:

javascript:(window.setTimeout(function() { alert('i ran'); }, 1000));void(0);
epoch
  • 16,396
  • 4
  • 43
  • 71
2

When you use the javascript: protocol in an address bar (which is what all bookmarklets do), the browser does a document.write on whatever the return value is if it's truthy.

A setTimeout call always returns a number for the timer. To fix this you can either append a void(0); like epoch or as I like to do, wrap it in an IIFE:

(function() {
    window.setTimeout( function() {alert('i ran');}, 1000 );
})();
qwertymk
  • 34,200
  • 28
  • 121
  • 184