Solved

How to dynamically include code on a webpage?

Posted on 2014-09-11
7
149 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
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.

 

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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

773 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