Solved

How to dynamically include code on a webpage?

Posted on 2014-09-11
7
153 Views
Last Modified: 2014-09-22
I'm trying to figure out if this is even possible and if it is how? I want to dynamically add JS code to a page onload. The object is already created and I want to update the object in the updated code.
primary file contains:
var aa = {};    
aa.test ='orig';
var key = '111';
var stbi = document.createElement('script');
		 switch(key) {
		 case '111': 
		 	  stbi.src = "one.js";
			  break;
		  case '222':
		  	  stbi.src = "two.js";
			  break;
		  default:
		  	stbi.src = "three.js";
		  }

		  stbi.onload = function() {
		  document.head.appendChild(this);
		  }

Open in new window

and the test JS file (one.js) to be dynamically included contains:
aa.test = 'script one';
console.log('---->'+aa.test);

Open in new window

how would I accomplish this?
0
Comment
Question by:894359
  • 4
  • 3
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40317905
A quick one :
var script  = document.createElement("script");
script.src = "/path/to/one.js"; // path to your file
document.head.appendChild(script);

Open in new window


the same with jQuery :
$.getScript("/path/to/one.js");

Open in new window

0
 

Author Comment

by:894359
ID: 40317944
Hi Leakim... how is that different than what I did?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40317949
aha sorry, It's the same, what is the issue with yur code?
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:894359
ID: 40318130
Not getting the included js file to work?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40318221
you need to specify their path relatively to the page
0
 

Author Comment

by:894359
ID: 40318346
I did. the js file is in the same directory.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40318382
you need to have the script tag too

<script>
aa.test = 'script one';
console.log('---->'+aa.test);
</script>
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

830 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