PDA

View Full Version : Using Script Multiple Times



jeffc418
Mar 25th, 2007, 07:42 PM
Hi, I'm using the "Chained Selects" script from Dynamic Drive and I'm trying to use it more than once on one page (around six times). The problem is that there is a section of the code that goes in the <head> tags. If there's only one <head> section and six codes, how can I do that? Look below for sample. Thanks!


<html>

<head>
<title>Order a Custom Computer</title>

&lt;script language="javascript" src="C:\Users\Jeff\Documents\Other\HTML\YourPerfectPC.Com\Scripts\Processor\chainedselects.js">

/***********************************************
* Chained Selects script- By Xin Yang (http://www.yxscripts.com/)
* Script featured on/available at http://www.dynamicdrive.com/
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

</script>
&lt;script language="javascript" src="C:\Users\Jeff\Documents\Other\HTML\YourPerfectPC.Com\Scripts\Processor\exampleconfig.js"></script></head>

<body onload="initListGroup('vehicles', document.forms[0].make, document.forms[0].type, document.forms[0].model)">

....................

A section needs to go between head tags, but how can I use that code more than once? I've tried to just put it in there multiple times and then changed the OnLoad section (combined the two with a semicolon) and changed the second set to [1]'s instead of [0]'s, but it doesn't work, it's the same script, only with different SRCs. Take a look:

Here's the <head>(that doesn't work):


<html>

<head>

<title>Order A Custom Computer</title>

&lt;script language="javascript" src="C:\Users\Jeff\Documents\Other\HTML\YourPerfectPC.Com\Scripts\Processor\chainedselects.js">

/***********************************************
* Chained Selects script- By Xin Yang (http://www.yxscripts.com/)
* Script featured on/available at http://www.dynamicdrive.com/
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
</script>
&lt;script language="javascript" src="C:\Users\Jeff\Documents\Other\HTML\YourPerfectPC.Com\Scripts\Processor\exampleconfig.js">
</script>

&lt;script language="javascript" src="C:\Users\Jeff\Documents\Other\HTML\YourPerfectPC.Com\Scripts\Video Card\chainedselects.js">

/***********************************************
* Chained Selects script- By Xin Yang (http://www.yxscripts.com/)
* Script featured on/available at http://www.dynamicdrive.com/
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

</script>
&lt;script language="javascript" src="C:\Users\Jeff\Documents\Other\HTML\YourPerfectPC.Com\Scripts\Video Card\exampleconfig.js"></script>

</head>

Here's what the <head> looks like when it loads one of the scripts (and works):


<html>

<head>
<title>Order a Custom Computer</title>

&lt;script language="javascript" src="chainedselects.js">

/***********************************************
* Chained Selects script- By Xin Yang (http://www.yxscripts.com/)
* Script featured on/available at http://www.dynamicdrive.com/
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

</script>
&lt;script language="javascript" src="exampleconfig.js"></script>
</head>
I found that if I change the bodyOnLoad thing to:


<body onload="initListGroup('vehicles', document.forms[0].make, document.forms[0].type, document.forms[0].model);initListGroup('videocards', document.forms[1].make, document.forms[1].type, document.forms[1].model)">

The numbers change which ones it goes to, only the first one ([0]), doesn't show up. Please help, thanks.

Philip M
Mar 25th, 2007, 08:49 PM
You cannot have multiple scripts which have the same names for functions and/or variables. The second will over-write the first (and probably cause chaos as well).

If you want to do this you need to go through the scripts and change all the function and variable names from (say) x to (say) x1, x2, x3 etc.

And of course all the scripts must have different names.

jeffc418
Mar 27th, 2007, 12:17 AM
I got a reply from the creator of the script:


For each set on chained selects, you start the content definition with addListGroup(), and call initListGroup() in the body onload.


Any idea what that means?

EDIT: I'm new to Javascript, so please use simple terms, I dunno what function and variable names are. Thanks!

Philip M
Mar 27th, 2007, 08:28 AM
EDIT: I'm new to Javascript, so please use simple terms, I dunno what function and variable names are. Thanks!

Er, in that case you are trying to run before you can walk.