JavaScript 警报在 Firefox 6 中不起作用

发布于 2024-11-19 17:27:24 字数 240 浏览 0 评论 0 原文

栏中运行此 JavaScript 代码:

javascript:alert("Hello")

我尝试在 Firefox 6 的地址

ReferenceError:警报未定义。

它曾经在 Firefox 5 中运行良好,现在仍然可以在 Opera、Safari 和 Chrome 上运行。我该如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

在梵高的星空下 2024-11-26 17:27:25

如果您的可点击书签被损坏并且您想要恢复它,您可以创建一个可点击按钮,而不是使用自定义按钮 Firefox 扩展。

按钮相对于从 Scratchpad 运行的优点:

  • 您实际上可以保存书签(按钮),
  • 您可以拥有漂亮的自己的图标(创建一些图像,例如 PNG 文件,导入它并在新按钮对话框中对其进行 base64_encode)。

该扩展程序有点特殊,因为按钮在 Firefox chrome 级别运行,因此它们具有更多特权(您可以与浏览器的 API 进行交互),并且按钮之间没有一一对应的关系。普通的 JS 和按钮代码(需要一些调整)。更准确地说,从按钮看到的 documentwindow 并不是您所期望的。

但是,您可以将“好”windowdocument 分配给您的变量,然后处理这些变量(最好不要重新定义窗口;)

这是我编写的示例代码这在 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);

您必须将 theWindow. 放在它们之前,而不是直接使用全局函数(例如 setTimeoutalert) ,并替换document/window 与本地 theDocument/theWindow ,它似乎正在工作。然而,我还没有对非常复杂的情况进行彻底的测试。

要添加按钮,右键单击已有的任何按钮,然后选择“添加新按钮...”。

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...'.

携君以终年 2024-11-26 17:27:24

目前似乎不允许使用 javascript:data: URL(直接在地址栏中)根据此评论

仅供参考,我可能会将这个错误分为多个短期和长期修复。

短期:禁止将 javascript: URL 粘贴到 URL 栏中
长期来看:还要求将小书签列入“白名单”
书签管理器才能运行 JavaScript

并且这是在 ="http://www.mozilla.com/en-US/firefox/6.0beta/releasenotes/buglist.html">最新版本最后一条评论还指出:

javascript: 实际上并没有被忽略 - 它们会运行,但是在一个“空”上下文中,没有任何您期望的常见 DOM 方法,因此最常见的用途(例如 javascript:alert(1))只是抛出(因此实际上被忽略)。不过 javascript:1+1 工作得很好。

现在:

如何解决这个问题?

不能,你必须等到他们决定采取适当的解决方案。正如评论所说,小书签可以工作,但必须明确允许。如果您只想测试代码,请使用 Firebug 或新的 便签本功能。

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.

总以为 2024-11-26 17:27:24

Felix 的答案正确地说明了为什么 URL 栏中的 javascript: 不再起作用。

如果您尝试调试网页,则可以使用 Web 控制台(不要与错误控制台混淆)来替代它。在紧凑的菜单中,它位于 Web Developer 下;在完整菜单栏中,它位于“工具”下。或者您可以按 ctrl-shift-K(在 Mac 上为 cmd-shift-K)。带有大于号的横线是 JavaScript 提示符;在那里输入的代码将在当前页面的上下文中进行评估。单击该栏上方带有下划线的区域中的任何内容都可以打开检查器窗口。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文