Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include CDN in javascript? [duplicate]

I know how to include CDN in HTML file.

<!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.

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

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

like image 849
Kingston Avatar asked May 05 '17 04:05

Kingston


2 Answers

You can do it in 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);
like image 144
Nick Weseman Avatar answered Sep 17 '22 17:09

Nick Weseman


Using document.write you can include the 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")
    );
like image 41
Naga Sai A Avatar answered Sep 18 '22 17:09

Naga Sai A