Solved

How to dynamically include code on a webpage?

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

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

746 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

16 Experts available now in Live!

Get 1:1 Help Now