动态编写脚本时无法访问 Javascript 函数
所以我有一个外部 javascript 文件,我们将其称为 something.js
并将其记录到文档中。由于某些原因,我无法在 Safari 或 FireFox 中访问该功能。
它有点像这样:
<script type="text/javascript">
document.write(decodeURI("%3Cscript src='something.js' type='text/javascript'%3E%3C/script%3E"));
myFunction();
</script>
所以这告诉我 myFunction 为 null,但如果我将相同的函数调用放在 some.js 的末尾,它就会起作用。现在这不是确切的情况,所以我知道对于这种特殊情况,这不是最好的方法。
so I have an external javascript file, lets call it something.js
and i document.write it to the document. For some reason, I cant access the function in Safari or FireFox.
It goes a little something like this:
<script type="text/javascript">
document.write(decodeURI("%3Cscript src='something.js' type='text/javascript'%3E%3C/script%3E"));
myFunction();
</script>
so this tells me myFunction is null, but if i put that same function call at the end of something.js, it works. Now this isnt the exact scenario, so I am aware that for this particualr case, this is not the best way to go about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我是这样做的:
实际上,这仅在页面加载时才有意义。如果您以动态方式重复下载脚本,您可能需要采用不同的方法,即 JSONP。
Here's how I do it:
Really, this makes sense only at page load time. If you are repeatedly downloading script in a dynamic fashion, you probably want to take a different approach, namely JSONP.
我认为发生这种情况是因为
myFunction();
在包含它的整个 .js 文件加载到浏览器之前被调用,尝试延迟myFunction();
调用几秒钟,然后再次测试以确保这是问题所在。I think this happens because
myFunction();
is called before the whole .js file that contains it loaded in the browser, try to delaymyFunction();
call few seconds and test again to make sure that this is the problem.使用 document.write 打印脚本包含标记后,浏览器会在加载外部脚本之前继续执行(尝试调用
myFunction()
)。因此,您的脚本尚不可用。After you print the script inclusion tag using document.write the browser continues execution (trying to invoke
myFunction()
) before it loads external script. So, your script is not available yet.script
块放入文档的
head
(如果不是已经)
myFunction()
调用。myFunction()
调用放在不同的位置script
块位于较低位置该文件。
第 1 步是最佳实践,但我认为如果您在文档中的其他位置动态包含 JS 文件,任何浏览器都不会阻塞。
关键点是您可以'不要在执行包含操作的同一脚本块中使用包含的 JS 代码。
这在 Firefox 中有效,我不能保证 Safari 也能如此。
script
block in thehead
of your document (if it isn'talready)
myFunction()
call.myFunction()
call in a differentscript
block somewhere lower onthe document.
Step 1 is a best practice, but I don't think any browser will choke if you dynamically include the JS file at some other point in the document.
The key point is that you can't make use of included JS code from within the same script block that does the inclusion.
This works in Firefox, I can't vouch for Safari.
我通常不会写两个单独的答案,但这是一个非常不同的答案,它同样可能有效。
在某些内容的底部调用
something_ready()
.js。不要在something.js 中定义something_ready()
,而是在html 文档中定义它。所以没有计时器,没有不必要的检查。
这会起作用,它只是假设您可以访问something.js,但您可能无法访问。如果没有,我提供的其他答案也可以。
I wouldn't normally write two separate answers, but this a very different answer, that's just as likely to work.
Put a call to
something_ready()
at the bottom of something.js. Don't definesomething_ready()
in something.js, define it in your html document.So no timers, no unneeded checking.
That will work, it just assumes that you have access to something.js, which you may not. If not, the other answer I provided will work.
这有效吗?
Does this work?
最简单的方法是编写两个
当使用 document.write 时,文本将直接写入文档,就在
因此,如果您使用两个
在一个
The easiest way to do this is to write two <script> blocks.
When document.write is used, the text is written directly into the document, right after the <script> tag. Because of this, when a browser encounters a <script> tag, it waits for the script to finish loading before continuing down the page. Any document.write that happens in the loaded script might affect the rest of the page (for example, if you do "document.write('<div>')", it will affect the rest of the layout).
So, if you use two <script> tags, the first one is encountered by the browser, and it uses document.write to output a new <script> tag. As soon as that <script> tag is finished, the browser continues down the page, and immediately encounters the new, dynamically added <script> tag. This <script> tag tells the browser to load your external Javascript. The browser will wait to execute the second original <script> tag until this <script> tag has finished loading. Then your function myFunction will be available.
The reason doing it all in one <script> tag doesn't work is because document.write doesn't happen until after the <script> has finished running (myFunction isn't available yet). The reason it works with two <script> tags is because the browser waits until each <script> tag runs in succession, and document.write puts a new <script> tag in line.