简体   繁体   English

另一个加载后如何包含javascript文件?

[英]How to include a javascript file after another loads?

I have an iframe with id="iframe" Inside my footer of index.php, I want to include a javascript file after the iframe loads. 我在index.php的页脚中有一个id =“ iframe”的iframe,在iframe加载后,我想包含一个javascript文件。

So far I have: 到目前为止,我有:

<script>
$('#iframe').onload = function () {
    </script>
        <script src="/js/myjsfile.js"></script>
    <script>
}
</script>

However, the webpage is getting angry and saying: SyntaxError: Unexpected end of input (for the {) and SyntaxError: Unexpected token } How can I include these files after load of another? 但是,该网页很生气,并说:SyntaxError:意外的输入结尾(对于{)和SyntaxError:意外的标记}如何在加载另一个文件之后包括这些文件?

You should try this : 您应该尝试这样:

var s = document.createElement("script");
s.type = "text/javascript";
s.src = "http://somedomain.com/somescript";
$("head").append(s);

See this post for more information. 有关更多信息,请参见此帖子

Hope this will help ! 希望这会有所帮助!

You can use jQuery.getScript() and detect the load event for the iframe. 您可以使用jQuery.getScript()并检测iframe的加载事件。

 $('#iframe').on('load', function(e) { $.getScript('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js', function() { // library is loaded. Now I can start to use.; console.log('Now the library is loaded!'); }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <iframe id="iframe" src="https://dummyimage.com/200x200/000/fff" style="width: 200px;height: 200px"></iframe> 

A different approach can be based on adding the script element dynamically like in: 可以基于动态添加脚本元素的方法,例如:

 $('#iframe').on('load', function(e) { $('head').append($('<script/>', {src: 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js'})); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <iframe id="iframe" src="https://dummyimage.com/600x400/000/fff" style="width: 200px;height: 200px"></iframe> 

You can inject a script tag on iframe load like this. 您可以像这样在iframe负载上注入脚本代码。

        <script>
         $('#iframe').on('load', function() {
             var script = document.createElement('script');
             script.src = '/js/myjsfile.js';
             document.body.appendChild(script);
         });
        </script>

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

相关问题 页面加载前如何动态包含JavaScript文件 - How to dynamically include a javascript file, before the page loads 如何将 javascript 文件包含到另一个 javascript 文件中? - How to include a javascript file into another javascript file? 如何确保在事件侦听器触发后加载 Javascript 文件? - How to ensure Javascript file loads after event listener triggers? 如何在另一个 JavaScript 文件中包含一个 JavaScript 文件? - How do I include a JavaScript file in another JavaScript file? 如何使用 Vanilla Javascript 在另一个 html 文件中包含 html 文件? - How to include a html file inside another html file with Vanilla Javascript? 如何在纯 javascript 的另一个文件中包含 class 文件 - How to include a class file in another file in pure javascript 我如何使用javascript将文件的一部分包含在另一个文件中 - how can i include part of a file in another file with javascript Telerik包含他们的javascript后如何包含我的javascript文件 - How to include my javascript file after Telerik includes their javascript 将Javascript文件包含在另一个javascript文件中并加载 - Include Javascript file in another javascript file and Loading Javascript-重定向到另一个网站,并在该页面加载后调用Javascript函数 - Javascript - redirect to another website and call Javascript function after that page loads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM