如何在 javascript 文件 (*.js) 中包含 CDN?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43796705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:09:56  来源:igfitidea点击:

How to include CDN in javascript file (*.js)?

javascriptjquerybootstrap-dialog

提问by Kingston

I know how to include CDN in HTML file.

我知道如何在 HTML 文件中包含 CDN。

<!DOCTYPE html>
<html>
<head>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
</body>
</html>

But what I am trying to do is:
I want to include CDN like jquery in my javascript file.

但我想要做的是:
我想在我的 javascript 文件中包含像 jquery 这样的 CDN。

May be what I am trying to do is impossible.
Actually, I want to call BootstrapDialog.Confirmfrom my javascript file.
So, I want to include required CDN for BootstrapDialog in js file.
Then I can call BootstrapDialog.Confirm.

可能是我想要做的事情是不可能的。
实际上,我想从我的 javascript 文件中调用BootstrapDialog.Confirm
所以,我想在 js 文件中包含 BootstrapDialog 所需的 CDN。
然后我可以调用BootstrapDialog.Confirm

If my question is not reasonable, forgive me as I am a beginner.

如果我的问题不合理,请原谅我,因为我是初学者。

回答by Nick Weseman

You can do it in Javascript:

你可以在 Javascript 中做到:

var jQueryScript = document.createElement('script');  
jQueryScript.setAttribute('src','https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js');
document.head.appendChild(jQueryScript);

回答by Naga Sai A

Using document.write you can include the cdn

使用 document.write 你可以包含 cdn

 document.write(
      unescape("%3Cscript src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js' type='text/javascript'%3E%3C/script%3E")
    );