Solved

How to dynamically include code on a webpage?

Posted on 2014-09-11
7
156 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
[X]
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
  • 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS - Powershell 7 73
javascript delete path 7 42
Change javascript filter code from OR logic to AND 5 38
Javascript image manipulationcrop-image 7 38
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

738 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