简体   繁体   English

JavaScript 警报在 Firefox 中不起作用 6

[英]JavaScript alert not working in Firefox 6

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. ReferenceError:未定义警报。

It used to work fine in Firefox 5 though, and still works on Opera, Safari and Chrome.它曾经在 Firefox 5 中运行良好,但仍然适用于 Opera、Safari 和 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 :根据此评论,目前不允许使用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短期:不允许将 javascript: URL 粘贴到 URL 栏中
Longer term: additionally require that bookmarklets be "whitelisted" in the Bookmark Manager before it can run JavaScript长期:还需要在书签管理器中将书签“列入白名单”,然后才能运行 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 (eg javascript:alert(1)) just throw (and thus are effectively ignored). javascript:实际上并没有被忽略——它们在运行,但是在一个“空”的上下文中,没有任何你期望的常用 DOM 方法,所以最常见的用途(例如 javascript:alert(1))只是抛出(因此被有效地忽略)。 javascript:1+1 works fine, though.不过,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 Firebug or the new Scratchpad feature.如果您只想测试代码,请使用Firebug或新的Scratchpad功能。

Felix's answer correctly states why javascript: in the URL bar doesn't work any more. Felix 的回答正确地说明了为什么javascript:在 URL 栏中不再起作用。

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).如果您尝试调试web页面,则可以使用 Web 控制台(不要与错误控制台混淆)来替代它。 In the compact menu, it's under Web Developer;在精简菜单中,它位于 Web Developer 下; in the full menu bar, it's under Tools.在完整的菜单栏中,它位于工具下。 Or you can press ctrl-shift-K (cmd-shift-K on macs).或者您可以按 ctrl-shift-K(在 Mac 上为 cmd-shift-K)。 The bar with a greater-than sign is a JavaScript prompt;带有大于号的条是 JavaScript 提示符; 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.可以单击该条上方带下划线的区域中的任何内容以显示检查器 window。

If your clickable bookmarklet got broken and you want it back, you can create a clickable button instead using Custom Buttons Firefox extension.如果您的可点击小书签坏了并且您想要将其恢复,您可以使用自定义按钮Firefox 扩展来创建一个可点击按钮

The advantages of button over running from Scratchpad:按钮相对于 Scratchpad 运行的优势:

  • you can actually save the bookmarklet (button),您实际上可以保存小书签(按钮),
  • you can have a nice own icon (create some image eg 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 ).扩展有点特别,因为按钮运行在 Firefox chrome 级别,所以他们有更多的特权(你可以与浏览器的 API 交互),并且普通 JS 和按钮代码之间没有一对一的对应关系(它需要一些调整)。 More precisely, document and window seen from button are not the ones you were expecting.更准确地说,从按钮看到的documentwindow不是您所期望的。

However, you can assign the 'good' window and document to your variables, and then work on these variables instead (better not redefine window;)但是,您可以将“好” windowdocument分配给您的变量,然后改为处理这些变量(最好不要重新定义 window;)

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.而不是直接使用全局函数(如setTimeoutalert ),您必须将theWindow. before them, and replace document / window with local theDocument / theWindow and it's seems to be working.在他们之前,并用本地theDocument / theWindow替换document / window ,它似乎正在工作。 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...'.要添加按钮,请右键单击您已有的任何按钮,然后选择“添加新按钮...”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM