动态编写脚本时无法访问 Javascript 函数

发布于 2024-08-24 06:35:28 字数 427 浏览 2 评论 0原文

所以我有一个外部 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 技术交流群。

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

发布评论

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

评论(7

等风来 2024-08-31 06:35:28

我是这样做的:

<script type="text/javascript" language='javascript'>
  document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="something.js"><\/script>');
  var contentloadtag=document.getElementById("contentloadtag");
  contentloadtag.onreadystatechange=function(){
    if (this.readyState=="complete") { myFunction(); }
  }
</script>

实际上,这仅在页面加载时才有意义。如果您以动态方式重复下载脚本,您可能需要采用不同的方法,即 JSONP。

Here's how I do it:

<script type="text/javascript" language='javascript'>
  document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="something.js"><\/script>');
  var contentloadtag=document.getElementById("contentloadtag");
  contentloadtag.onreadystatechange=function(){
    if (this.readyState=="complete") { myFunction(); }
  }
</script>

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.

素罗衫 2024-08-31 06:35:28

我认为发生这种情况是因为 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 delay myFunction(); call few seconds and test again to make sure that this is the problem.

只为守护你 2024-08-31 06:35:28

使用 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.

深海夜未眠 2024-08-31 06:35:28
  1. 将动态 script 块放入
    文档的 head(如果不是
    已经)
  2. 删除 myFunction() 调用。
  3. myFunction() 调用放在不同的位置
    script 块位于较低位置
    该文件。

第 1 步是最佳实践,但我认为如果您在文档中的其他位置动态包含 JS 文件,任何浏览器都不会阻塞。

关键点是您可以'不要在执行包含操作的同一脚本块中使用包含的 JS 代码。

这在 Firefox 中有效,我不能保证 Safari 也能如此。

<html>
    <head>
        <script type="text/javascript">
            document.write(decodeURI("%3Cscript src='something.js' type='text/javascript'%3E%3C/script%3E"));
        </script>
    </head>
    <body>
        <script type="text/javascript">
            myFunction();
        </script>
    </body>
</html>
  1. Put your dynamic script block in the
    head of your document (if it isn't
    already)
  2. Remove the myFunction() call.
  3. Put myFunction() call in a different
    script block somewhere lower on
    the 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.

<html>
    <head>
        <script type="text/javascript">
            document.write(decodeURI("%3Cscript src='something.js' type='text/javascript'%3E%3C/script%3E"));
        </script>
    </head>
    <body>
        <script type="text/javascript">
            myFunction();
        </script>
    </body>
</html>
も让我眼熟你 2024-08-31 06:35:28

我通常不会写两个单独的答案,但这是一个非常不同的答案,它同样可能有效。

在某些内容的底部调用 something_ready() .js。不要在something.js 中定义something_ready(),而是在html 文档中定义它。

所以没有计时器,没有不必要的检查。

    <script type="text/javascript">
        document.write(decodeURI("%3Cscript src='something.js' type='text/javascript'%3E%3C/script%3E"));

        function something_ready(){
            myFunction();
        }
    </script>

这会起作用,它只是假设您可以访问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 define something_ready() in something.js, define it in your html document.

So no timers, no unneeded checking.

    <script type="text/javascript">
        document.write(decodeURI("%3Cscript src='something.js' type='text/javascript'%3E%3C/script%3E"));

        function something_ready(){
            myFunction();
        }
    </script>

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.

回首观望 2024-08-31 06:35:28

这有效吗?

<script type="text/javascript">
document.write(unescape("%3Cscript src='something.js' type='text/javascript'%3E%3C/script%3E"));
document.write("<"+"script type='text/javascript>myFunction();<"+"/script>");
</script>

Does this work?

<script type="text/javascript">
document.write(unescape("%3Cscript src='something.js' type='text/javascript'%3E%3C/script%3E"));
document.write("<"+"script type='text/javascript>myFunction();<"+"/script>");
</script>
乖乖 2024-08-31 06:35:28

最简单的方法是编写两个

<script type="text/javascript">
    document.write(decodeURI("%3Cscript src='something.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
    myFunction();
</script>

当使用 document.write 时,文本将直接写入文档,就在

因此,如果您使用两个

在一个

The easiest way to do this is to write two <script> blocks.

<script type="text/javascript">
    document.write(decodeURI("%3Cscript src='something.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
    myFunction();
</script>

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.

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