Solved

Add JavaScript file using a JavaScript function

Posted on 2014-03-07
7
278 Views
Last Modified: 2014-03-11
Is there a way to dynamically add a JavaScript file to a webpage using a JavaScript function?

The method in this link looks promising, but doesn't seem to work: How do I include a Javascript file in a Javascript file?

My code is posted below.  What am I dong wrong?

Of course if I add this line to the HTML file, it will work.  But I am trying to learn a way to do this dynamically.
<script type="text/javascript" src="another.js"></script>

Open in new window

test.html
test.js
another.js
0
Comment
Question by:anAppBuilder
  • 3
  • 3
7 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Because by the time your code has run the js file is still loading - js doesn't wait for external events, it carries on running.
Simple solution is to add a call in the dynamic js to call the testExternalSource function and not in the parent page or add a callback in your function to run when it is loaded.
0
 
LVL 9

Expert Comment

by:Sar1973
Comment Utility
You should assign the script an id and launch getElementById("myScript").src="myFile.js"; I guess.
0
 

Author Comment

by:anAppBuilder
Comment Utility
Thank you both.

Gary, can you explain what you mean by "add a call in the dynamic js to call the testExternalSource function and not in the parent page "?  

I understand the callback approach, but would prefer not to have all added code be running in callbacks.

Sar1973 I tried adding this line to test.html
<script id = "ext" type="text/javascript" src=""></script>

Open in new window

and changing function addJavaScript() to
document.getElementById("ext").src ="another.js";

Open in new window

but that does not work.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 58

Expert Comment

by:Gary
Comment Utility
In your master page you have
function testAnotherJs() {
	addJavaScript();
	testExternalSource();
}

Open in new window

Remove testExternalSource(); and put it in your dynamic js file
testExternalSource();
function getExternalString() {
	var extString = "A string from another.js";
	return extString;
}

Open in new window


The other way is to attach a callback to the script load
http://www.nczonline.net/blog/2009/07/28/the-best-way-to-load-external-javascript/
It still means you cannot run your testAnotherJs function as it is
0
 

Author Comment

by:anAppBuilder
Comment Utility
Thank you again, Gary.  I moved my testExternalSource function to the second .js and that does not solve the problem.

My code is attached.
test.html
test.js
another.js
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
Comment Utility
test.html
<!doctype html>
<html>
<head>
<script type="text/javascript" src="test.js"></script>

</head>

<body onload="addJavaScript()">
	<p id="p4"> Test Paragraph FOUR.  Test of external JavaScript source.</p>
</body>
</html>

Open in new window

test.js
function addJavaScript() {
	 var head = document.getElementsByTagName('head')[0];
	 script = document.createElement('script');
	 script.src = "another.js";
	 script.type = 'text/javascript';
	 head.appendChild(script);
}

Open in new window

another.js
function getExternalString() {
	var extString = "A string from another.js";
	return extString;
}

function testExternalSource() {
	var extString = getExternalString();
	document.getElementById("p4").innerHTML += extString + "<br>";
}
testExternalSource()

Open in new window

0
 

Author Closing Comment

by:anAppBuilder
Comment Utility
Beautiful...Thank you!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now