Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Add JavaScript file using a JavaScript function

Posted on 2014-03-07
7
290 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
ID: 39913822
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
ID: 39914612
You should assign the script an id and launch getElementById("myScript").src="myFile.js"; I guess.
0
 

Author Comment

by:anAppBuilder
ID: 39916542
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 58

Expert Comment

by:Gary
ID: 39916554
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
ID: 39922353
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
ID: 39922363
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
ID: 39922408
Beautiful...Thank you!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dynamic Dropdowns 15 32
function parameter and using that as array 15 22
Making an "H1" a specific color only on a specific page. 3 27
Insert Button on a table 16 37
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

840 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