474,044 Members | 3,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conditionally Loading .js files.

3 New Member
Hi

I would like to know how to load .js files conditionally?
Suppose I have a Drop down box that is populated dynamically with A B C.
When I select Element A I want it to load a .js file, B a different .js file and C a different .js. The js. files are not simply A.js, they are a reference to an URL address where they are stored.

Expand|Select|Wrap|Line Numbers
  1. <select size='1'   
  2. onchange="var si=this.selectedIndex; if(si){ addScript(  this.options[si].value)}">
  3.     <option>Select an Option...</option>
  4.     <option value="temp.js">A</option>
  5.     <option value="whatever.js">B</option>
  6.     <option value="whatever2.js">C</option>
  7. </select>
  8.  
  9. <script>
  10. function addScript(turl) {
  11.     var xJs = document.createElement("script");
  12.     xJs.type = "text/javascript";
  13.     var h = document.getElementsByTagName("head");
  14.     if (h && h[0]) {
  15.         h[0].appendChild(xJs);
  16.     }
  17.     xJs.src = turl;
  18.     return xJs;
  19. }
  20.  
  21. </script>
Any ideas on how to make this work?
Feb 6 '08 #1
2 2128
mrhoo
428 Contributor
"var si=this.selecte dIndex; if(si){ addScript( this.options[si].value)}";
the value of a select with size='1' is the value of its selected option.

onchange= "addScript(this .value)"
or get it out of the html and assign it onload-

Expand|Select|Wrap|Line Numbers
  1. onload= function(){
  2.     var el= document.getElementsByTagName('select')[0];// or use an id
  3.     el.onchange= function(e){
  4.         e= window.event? event.srcElement : e.target
  5.         return addScript(e.value);
  6.     }
  7. }
Feb 6 '08 #2
dohko8
3 New Member
This is where i need it to work.


Expand|Select|Wrap|Line Numbers
  1.          <SELECT name=_fid_6 onChange="var si=this.selectedIndex; if(si){addScript(this.options[si].value)}; populateDynamicDropDownList(this, document.forms[0]._fid_7,1)";>
  2.           <OPTION selected>Type</OPTION>
  3.          </SELECT>
Do i need to change the Addscript function?
I need it to load for example. www.abcd.com/a.js
when i select option 1. and then based on this run the second function. populateDynamic DropDownList
can this be done?


the value of a select with size='1' is the value of its selected option.

onchange= "addScript(this .value)"
or get it out of the html and assign it onload-

Expand|Select|Wrap|Line Numbers
  1. onload= function(){
  2.     var el= document.getElementsByTagName('select')[0];// or use an id
  3.     el.onchange= function(e){
  4.         e= window.event? event.srcElement : e.target
  5.         return addScript(e.value);
  6.     }
  7. }
Feb 7 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
5218
by: Ram | last post by:
We are trying to load text tab delimited files into SQL server using informatica 5.1 version. The text files are FTPed to a local server and then files are moved to applicable folder before loading. The problem is when we open the files after FTP the characters are already converted to something else. EX: ü got converted as Ï. Now when we load them into SQL server we want Ï to transfer as ü again.
3
1744
by: alwayswinter | last post by:
I currently have a form where a user can enter results from a genetic test. I also have a pool of summaries that would correspond to different results that a user would enter into the form. I ideally I would like to have the user enter their results and then have a master summary created with the different word or HTML files on submit. (condition 1 + condition 2 = combining/displaying corresponding HTML files) I've gotten as far as...
3
3805
by: Nevyn Twyll | last post by:
I have a bunch of test case classes (.cs) files in my executable. For security and bloat reasons, I don't want them to compile into a Release executable - only into a Debug build. How do I conditionally exclude files or classes from my Release builds? In old C++ we would use #ifdef ... #endif statements, but we don't have pragmas in C# (or don't seem to). Help?
2
1767
by: Paul | last post by:
Just wondering if anyone knows how to conditionally load a user control on a page? In other words load the user control if the page is reached with one link and do not load it if it is reached by another link. Thanks, -- Paul G Software engineer.
2
1265
by: Craig | last post by:
Is there a way to conditionally include a file in the HTML? <TD> if x= 1 then <!--#INCLUDE FILE="../File1"--> else <!--#INCLUDE FILE="../File2"--> end if
2
1318
by: Felix | last post by:
Hello, I use a #define like WITH_MYSTUFF to conditionally compile code within #ifdef WITH_MYSTUFF. I would now like the same define to conditionally link with a library. In the linker settings of VS.NET 2003 I have 'Input -> Additional Dependencies' where I can add my .lib files the linker should use. How do I tell the linker (by a macro ?) somthing like this:
6
2788
by: Venkatesh | last post by:
Hello All, I have couple of doubts regarding the concept of on-demand javascript loading using javascript code. I could see on the net different techniques for achieving this - techniques like: 1. document.write("<script src= language='JavaScript'></script>); 2. sc = document.createElement("<script>"); sc.setAttribute("src", ); and append this to the head
5
5136
by: Pete Marsh | last post by:
Wondering if anyone can recomend some sample code for dynamically loading the GD module. I have tried setting the extension dir in php.ini, and loading the GD module from there when apache is started, but it won't load. There is also the option of dynamically loading the module at run time of the script. LD() i believe is the call.
15
3713
by: pdhb_1 | last post by:
I'm trying to run Apache2.2.6 with php. However, I keep getting a long list of error messages in the log file indicating that includes are failing. For example, "PHP Warning: PHP Startup: Unable to load dynamic library './ext/ php_sybase_ct.dll' - The specified module could not be found.\r\n in Unknown on line 0" I have changed the "extension_dir" line in the php.ini file every way I can think of, including the original:
0
10337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
12140
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11602
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
12022
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8698
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7868
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
5416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.