I am trying to load library script files dynamically, iff they are not loaded by any other page. Here, container
is just a div element.
if($ === undefined ){
$(".container").append('<script src="../static/js/jquery.js"></script>')
$(".container").append('<script src="../static/js/dt_jq_ui.js"></script>');
$(".container").append('<script src="../static/js/report_app.js"></script>')
}
But it fails and the firefox console error is,
Error text is,
SyntaxError: unterminated string literal
$(".container").append('<script src="../static/js/jquery.js">
Why is the html string with script
tag marked as invalid?
Does it event enter the
if block
?
Answer is no
because $
is treated like a variable
1.Variables that are actually 'not defined', i.e. they don't exists as a given name isn't bound in the current lexical environment. Accessing such a variable will throw an error, but using typeof won't and will return 'undefined'. In contrast, accessing non-existing properties will not throw an error and return undefined instead (and you may use the in operator or the hasOwnProperty() method to check if properties actually do exist).
2.Existing variables which have not been assigned a value (which is common because of var hoisting) or which have been explicitly set to undefined. Accessing such a variable will return undefined, typeof will return 'undefined'.
3.Existing variables which have been explicitly set to null. Accessing such a variable will return null, typeof will return 'object'. Note that this is misleading: null is not an object, but a primitive value of type Null (which has the consequence that you can't return null from constructor functions - you have to throw an error instead to denote failure).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments