JavaScript alert not working in Firefox 6

17,413

Solution 1

It seems using javascript: and data: URLs (directly in the address bar) are currently not allowed as per this comment:

FYI, I'm probably going to split this bug into multiple, short and longer term fixes.

Short term: disallow pasting of javascript: URLs into the URL bar
Longer term: additionally require that bookmarklets be "whitelisted" in the Bookmark Manager before it can run JavaScript

And this is the "bug" that was resolved in the latest version. The last comment also states:

javascript: is not actually ignored - they're run, but in an "empty" context that doesn't have any of the usual DOM methods you would expect, so most common uses (e.g. javascript:alert(1)) just throw (and thus are effectively ignored). javascript:1+1 works fine, though.

Now:

How do I fix this?

You can't, you have to wait until they decided for a proper solution. As the comment said, bookmarklets will work, but must be explicitly allowed. If you just want to test code, use either Firebug or the new Scratchpad feature.

Solution 2

Felix's answer correctly states why javascript: in the URL bar doesn't work any more.

The replacement for this, if you're trying to debug your web page, is the Web Console (not to be confused with the Error Console). In the compact menu, it's under Web Developer; in the full menu bar, it's under Tools. Or you can press ctrl-shift-K (cmd-shift-K on macs). The bar with a greater-than sign is a JavaScript prompt; code entered there will be evaluated in the context of the current page. Anything in the area above that bar that's underlined can be clicked on to bring up an inspector window.

Solution 3

If your clickable bookmarklet got broken and you want it back, you can create a clickable button instead using Custom Buttons Firefox extension.

The advantages of button over running from Scratchpad:

  • you can actually save the bookmarklet (button),
  • you can have a nice own icon (create some image e.g. PNG file, import it and base64_encode it inside the new button dialog).

The extension is a bit special because the buttons run at Firefox chrome level, so they're a bit more privileged (you can interact with the browser's API), and there's no 1-to-1 correspondence between normal JS and the button code (it needs some tweaking). More precisely, document and window seen from button are not the ones you were expecting.

However, you can assign the 'good' window and document to your variables, and then work on these variables instead (better not redefine window;)

Here's a sample code I written which works pretty well in Fx10:

// get proper 'window' and 'document' into our private variables
var theWindow = window.top.getBrowser().selectedBrowser.contentWindow;
var theDocument = theWindow.document;

// here we go
var input = theDocument.getElementById("foo");
input.focus(); // just to show you it's working, unnecessary in fact

// simulate keyboard event
var evt = theDocument.createEvent("KeyboardEvent");
evt.initKeyEvent ("keypress", true, true, theWindow ,
            0, 0, 0, 0, 0, 65); // 65 == "A"
input.dispatchEvent(evt);

// show alert after 2 sec
theWindow.setTimeout( function(){
  input.value += "B";
  theWindow.alert(input.value); // alerts "AB"
},2000);

Instead of using global functions directly (like setTimeout, or alert), you have to put theWindow. before them, and replace document/window with local theDocument/theWindow and it's seems to be working. I haven't tested it thoroughly however with very complicated cases.

To add a button, right click on any button you already have and choose 'Add new button...'.

Share:
17,413
Ryan
Author by

Ryan

Updated on June 07, 2022

Comments

  • Ryan
    Ryan almost 2 years

    I tried running this JavaScript code in the address bar in Firefox 6:

    javascript:alert("Hello")
    

    I get a

    ReferenceError: alert not defined.

    It used to work fine in Firefox 5 though, and still works on Opera, Safari and Chrome. How do I fix this?

  • Quentin
    Quentin almost 13 years
    Two people referencing the same bug report within 25 seconds of each other. :D I've deleted my answer as this is more detailed.
  • GeReV
    GeReV over 12 years
    This "bug fix" just ruined a 2 months project I was working on. :(
  • gcb
    gcb over 12 years
    i have to agree with 80% on the comments on that bug. This is the dumbest solution for the problem. let's disable javascript on the browser too. that will make the world more secure. better yet, lets forbid the user to visit pages. that should solve all the hate speach on the internet!
  • Neal Ehardt
    Neal Ehardt about 12 years
    ** command+alt+K on macs. Sad day, I'm going to miss typing javascript in the address bar...