Load javascript file dynamically by appending script tag to body

Saravana Kumar

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, enter image description here

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?

ozil

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to load a Javascript file without Script tag?

Javascript how to create a script tag dynamically without the source file?

Dynamically load a JavaScript file

How can I dynamically load a image in the body tag in each component?

Javascript file does not load dynamically

JQuery to load Javascript file dynamically

How to call the Javascript file inside Body tag

Load external javascript through script tag

How to load Javascript file in a Service Worker dynamically?

How to dynamically load and use/call a JavaScript file?

How to Dynamically Load Javascript File into HTML

Dynamically load Javascript file one at a time

Dynamically insert script tag?

Script tag not connecting javascript to html file

How to add javascript script tag in XSL file

dynamically load script in top of file before other contents get executed

How to load the script file immediately (dynamically added) before continue?

Google Tag Manager - Tag Configuration - appending <script> content within <script>

why is my option tag not appending on dynamically generated select tag

How to load an html file with <script> tag on a node js server?

Ensure script tag is included as first tag in the body?

Is it wrong to place the <script> tag after the </body> tag?

JavaScript: Appending same text to multiple tag in HTML

Appending the javascript onclick event to anchor tag

Javascript issue in firefox appending a link tag

Dynamically load external javascript file from Angular component

How to dynamically load a JAR file with Vert.x JavaScript?

Dynamically change the <script> tag src

Appending array to JSON file with JavaScript