JavaScript 警报在 Firefox 6 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6643414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript alert not working in Firefox 6
提问by Ryan
I tried running this JavaScript code in the address bar in Firefox 6:
我尝试在 Firefox 6 的地址栏中运行此 JavaScript 代码:
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?
虽然它曾经在 Firefox 5 中运行良好,但仍然可以在 Opera、Safari 和 Chrome 上运行。我该如何解决?
采纳答案by Felix Kling
It seems using javascript:
and data:
URLs (directly in the address bar) are currently not allowed as per this comment:
根据此评论,目前似乎不允许使用javascript:
和data:
URL(直接在地址栏中):
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
仅供参考,我可能会将这个错误分成多个短期和长期修复。
短期:禁止将 javascript:URL 粘贴到 URL 栏中
长期:另外要求在书签管理器中将书签列入“白名单”,然后才能运行 JavaScript
And this is the "bug"that was resolved in the latest version. The last commentalso 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.
javascript: 实际上并没有被忽略——它们是运行的,但是在一个“空”的上下文中,它没有你期望的任何常见的 DOM 方法,所以最常见的用法(例如 javascript:alert(1))只是抛出 (因此被有效地忽略)。javascript:1+1 工作正常,不过。
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 Firebugor the new Scratchpadfeature.
你不能,你必须等到他们决定一个合适的解决方案。正如评论所说,书签可以工作,但必须明确允许。如果您只想测试代码,请使用Firebug或新的Scratchpad功能。
回答by zwol
Felix's answer correctly states why javascript:
in the URL bar doesn't work any more.
Felix 的回答正确地说明了为什么javascript:
URL 栏中不再起作用。
The replacementfor 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.
在更换了这一点,如果你想调试你的网页,是Web控制台(不要用错误控制台相混淆)。在紧凑菜单中,它位于 Web Developer 下;在完整菜单栏中,它位于工具下。或者您可以按 ctrl-shift-K(在 Mac 上按 cmd-shift-K)。带有大于号的栏是 JavaScript 提示;在那里输入的代码将在当前页面的上下文中进行评估。可以单击该栏上方带下划线的区域中的任何内容以显示检查器窗口。
回答by jakub.g
If your clickable bookmarklet got broken and you want it back, you can create a clickable buttoninstead using Custom ButtonsFirefox extension.
如果您的可点击书签坏了并且您想要它回来,您可以使用自定义按钮Firefox 扩展创建一个可点击按钮。
The advantages of button over running from Scratchpad:
按钮优于从 Scratchpad 运行的优点:
- you can actually savethe 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).
- 您实际上可以保存书签(按钮),
- 您可以拥有一个漂亮的自己的图标(创建一些图像,例如 PNG 文件,将其导入并在新按钮对话框中对其进行 base64_encode)。
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.
扩展有点特别,因为按钮是在 Firefox chrome 级别运行的,所以他们更有特权(可以与浏览器的 API 交互),并且普通 JS 和按钮代码之间没有一对一的对应关系(它需要一些调整)。更确切地说,document
并且window
从按钮看到没有您所期望的人。
However, you can assign the 'good' window
and document
to your variables, and then work on these variables instead (better not redefine window;)
但是,您可以将“好”window
和分配document
给您的变量,然后处理这些变量(最好不要重新定义窗口;)
Here's a sample code I written which works pretty well in Fx10:
这是我编写的示例代码,它在 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.
而不是直接使用全局函数(如setTimeout
, 或alert
),您必须放在theWindow.
它们之前,并将document
/替换window
为本地theDocument
/theWindow
并且它似乎正在工作。我还没有对它进行彻底的测试,但是情况非常复杂。
To add a button,right click on any button you already have and choose 'Add new button...'.
要添加按钮,请右键单击您已有的任何按钮,然后选择“添加新按钮...”。