JavaScript alert not working in Firefox 6
I tried running this JavaScript code in the address bar in Firefox 6:
javascript:alert("Hello")
I get a
开发者_Go百科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?
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.
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.
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...'.
精彩评论