如何在运行时添加脚本? (document.write不是函数)
我尝试在运行时将脚本添加到我的 Xul 应用程序中,
document.write('<script type="text/javascript" src="chrome://myapp/content/myscript.js"/>');
但我得到:
错误:document.write 不是 功能
确实存在于文档中。我还从文档中得到了这个例子:
document.open();
document.write("<h1>Out with the old - in with the new!</h1>");
document.close();
但我得到了:
document.open 不是一个函数
知道出了什么问题吗?
--update
也许我可以将我的 js 代码放入 Javacript 模块中并使用 Components.utils.import(查找放置资源的位置以及如何引用它)
I trying to add a script to my Xul application at runtime, with:
document.write('<script type="text/javascript" src="chrome://myapp/content/myscript.js"/>');
but I got:
Error: document.write is not a
function
The function does exist in the docs. I got also this example from the docs:
document.open();
document.write("<h1>Out with the old - in with the new!</h1>");
document.close();
but I got:
document.open is not a function
Any idea what's wrong?
--update
Maybe I could put my js code in a Javacript Module and import it with Components.utils.import (looking for where to put the resource and how to reference it)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您使用 XHTML,则
document.write
函数不可用;请参阅:http://www.w3.org/MarkUp/2004/xhtml-faq #docwrite。您可能正在使用 XHTML。您应该评估是否确实需要使用 XHTML。如果没有,那么您将能够使用
document.write
。但如果这样做,您将需要使用另一种方法来操作 DOM。您可以直接使用 DOM 方法来完成此操作,也可以使用辅助库(例如 jQuery)。The
document.write
function is not available if you are using XHTML; see: http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite. It is likely that you are using XHTML.You should evaluate whether you actually need to use XHTML. If not, then you will be able to use
document.write
. But if you do, you'll need to use an alternate approach to manipulating the DOM. You can do it directly using DOM methods, or you could use a helper library such as jQuery.来自这个示例。
From this example.
你的脚本标签里有javascript吗?您是否在代码中的某个位置启动该函数?它不会自行运行。
这有效:
Do you have the javascript inside a script tag? And do you initiate the function somewhere in the code? It doesn't run it self.
This works:
您在另一个答案的评论中说,您正在从 setTimeout 调用的函数中执行 document.write() ,该函数本身位于 onload 中。 我建议不要在页面加载后使用 document.write()。 根据 Mozilla doco:
并且由于自动 document.open()
从内存中我认为如果您在页面加载后使用 document.write() ,旧版本的 IE 只会给出错误,但我懒得尝试在中查找任何内容他们的多科。
正如其他人提到的,它不适用于 xhtml。
为什么你甚至想在这种情况下执行 document.write() ?为什么不直接包含您的脚本呢?
You said in a comment on another answer that you were doing the document.write() in a function called from a setTimeout that itself was in the onload. I'd recommend against using document.write() after the page has loaded. According to the Mozilla doco:
And because of the automatic document.open()
From memory I thought older versions of IE just gave an error if you used document.write() after the page had loaded, but I can't be bothered trying to find anything in their doco.
And as somebody else mentioned, it doesn't work with xhtml.
Why do you even want to do the document.write() in this instance? Why not just include your script directly?
有两种方法可以在运行时动态加载脚本。
第一种方法是通过下标加载器。
第二种方法是创建一个覆盖层来加载脚本并动态加载覆盖层 。
There are two ways to dynamically load a script at runtime.
The first way is via the subscript loader.
The second way is to create an overlay that loads your script and dynamically load the overlay.