If I include a script with an external source and then try to parse using jQuery - it will download the script, but won't load it into the DOM. I'm using using .append()
, but this seems to be the case for any other jQuery DOM insertion method.
Here's an example in Stack Snippets
<!-- begin snippet: js hide: false --> <!-- language: lang-js -->$(function(){
var selet2Html =
'<link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css" rel="stylesheet"/>' +
'<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.js" ><\/script>' +
'<select class="select2">' +
' <option value="AK">Alaska</option>' +
' <option value="HI">Hawaii</option>' +
'</select>' +
'<script >' +
' $(function() {' +
' $(".select2").select2();' +
' });' +
'<\/script>';
$("body").append(selet2Html);
});
<!-- language: lang-html -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<!-- end snippet -->
Even though this will download the script in the network tab:
It will still return the following error (indicating that the external library - select2 - never loaded):
I'd prefer not to use $.getScript()
as I'm actually loading this string dynamically with .load()
, so I'm not sure if it's going to contain any external scripts or not.
How can I get it to load the external library?