简体   繁体   English

我可以在运行时将JavaScript文件包含在另一个中吗?

[英]Can I include a JavaScript file inside another one at runtime?

I need to include a JavaScript file inside another one and use its functions at runtime. 我需要在另一个JavaScript文件中包含一个JavaScript文件并在运行时使用它的函数。 The file may exist in my server or in another one. 该文件可能存在于我的服务器或另一个服务器中。

How can I do this? 我怎样才能做到这一点?

var sc=document.createElement('script');
sc.src="script.js";
document.getElementsByTagName('head')[0].appendChild(sc);

Use something like this 使用这样的东西

the answer of this question you can find it in the following : Here 你可以在下面找到这个问题的答案: 这里

something like that visit the article to know more : 类似的东西访问文章了解更多:

<script type="text/javascript">
// Function to allow one JavaScript file to be included by another.
// Copyright (C) 2006-08 www.cryer.co.uk
function IncludeJavaScript(jsFile)
{
  document.write('<script type="text/javascript" src="'
    + jsFile + '"></scr' + 'ipt>'); 
}
</script>

and here are some links on stack overflow : 这里有一些关于堆栈溢出的链接:

One Two

With JQuery power do 用JQuery做电源吧

$.ajax({
  url: yourURL,
  dataType: "script",
  async: false
});

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

相关问题 我可以在JavaScript中包含其他源文件吗? - Can I include another source file in JavaScript? 我如何使用javascript将文件的一部分包含在另一个文件中 - how can i include part of a file in another file with javascript 如何在单独的文件中创建一个javascript库并将其“包含”在另一个文件中? - How can I create a javascript library in a separate file and “include” it in another? 如何包含多个JavaScript文件? - How can I include more than one Javascript file? 有没有办法在另一个JS文件中包含一个Javascript文件? - Is There Any Way To Include a Javascript File Inside Another JS File? 如何使用 Vanilla Javascript 在另一个 html 文件中包含 html 文件? - How to include a html file inside another html file with Vanilla Javascript? 在服务器端环境或命令行环境中,将一个javascript文件“包含”到另一个 - “Include” one javascript file to another one in serverside environment or in commandline environment 如何将我的Bundle中的javascript文件包含到twig文件中? - How can I include a javascript file from inside my Bundle into a twig file? 如何重用另一个JavaScript文件中的代码? - How can I reuse code from one JavaScript file in another? 如何使用另一个Javascript文件中的函数? - How can I use a function from one Javascript file in another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM