?
Solved

How to dynamically include code on a webpage?

Posted on 2014-09-11
7
Medium Priority
?
169 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
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 

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 2000 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

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

777 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