PDA

View Full Version : script activated from drop down box



martyn1987
07-16-2007, 06:24 PM
hey guys this may be really simple

i have a drop down box on my webpage and i need it to run a script when the value changes

but the script it runs depends on the value of the selected item in the drop down box

any idea on how i could do this with minimal coding.... or not what ever is easy lol


thanks in advance

Twey
07-16-2007, 06:25 PM
<select onchange="doSomething(this.value);">

martyn1987
07-16-2007, 06:44 PM
ooo never thought of that thanks

how do i then get the scritpt to load another script from a js file , i know how to do the onload do-dah , but how can i get it to do it from a function :S am at a lil loss here

thanks again in advance

Twey
07-16-2007, 07:27 PM
<script type="text/javascript">
var loadScript = (function() {
var head = document.getElementsByTagName("head")[0];
return function(url) {
var s = document.createElement("script");
s.type = "text/javascript";
s.src = url;
return head.appendChild(s);
};
})();
</script>

<select onchange="loadScript(this.value);">
<option value="scripts/fish.js">Fish</option>
<option value="cabbage.js">Cabbage</option>
<option value="http://www.otherserver.com/somescript.js">Remote Script</option>
</select>

martyn1987
07-16-2007, 08:17 PM
am having a lil trouble implementing this :s

it doesnt seem to be running my scritps when the value changes

anything you can thing of

Twey
07-16-2007, 08:41 PM
What's the code you're using?

martyn1987
07-16-2007, 08:48 PM
<script type="text/javascript">
var loadScript = (function() {
var head = document.getElementsByTagName("head")[0];
return function(url) {
var s = document.createElement("script");
s.type = "text/javascript";
s.src = url;
return head.appendChild(s);
};
})();
</script>
<body onload="numbers();winners();date()">


<select onchange="loadScript(this.value);">
<option value="archive/06 July 07 onewish.js">06 July 07</option>
<option value="archive/13 July 07 onewish.js">13 July 07</option>
</select>


basically what you gave me with the body onload i was using when i was calling the functions from a known script , ie one single script not a selection

Twey
07-16-2007, 09:22 PM
Hmm... maybe if we escape the URL. There's a memory leak in IE in that code too, I just realised.
function loadScript(url) {
var s = document.createElement("script");
s.type = "text/javascript";
s.src = encodeURI(url);
return document.getElementsByTagName("head")[0].appendChild(s);
};Note that since the document has already loaded, document.write() and similar things won't work, if you've used any of those.

martyn1987
07-18-2007, 10:23 AM
doesnt seem to work


i am using
document.getElementById('data1').innerHTML = 'name'

to change large amount of data, this code is generated by a program i wrote , if you know of another way of doing this then i am open to sugetions

thanks again in advance

Twey
07-18-2007, 11:35 AM
"Doesn't work" is a poor description of the problem. What does it do? What doesn't it do? What errors do you get?

innerHTML is bad (use the DOM methods (http://developer.mozilla.org/en/docs/Gecko_DOM_Reference) instead), but that's not the problem in this case -- unless you're using XHTML it should still work.

martyn1987
07-18-2007, 11:39 AM
lol sorry

when the pages loads up, it doesnt seem to run the script i programed ie it doesnt change the blank values to actual values

when the value in the drop down box is changed nothing seems to happen, i cant see the effect of any script/s

there are no error messages

hope this helps

Twey
07-18-2007, 11:53 AM
May we see the page in question?

martyn1987
07-18-2007, 12:00 PM
yeah here is the link

http://www.onewish.net/archive.html

thanks

Twey
07-18-2007, 12:05 PM
That would be because the scripts you're trying to include don't actually exist (http://www.onewish.net/archive/06&#37;20July%2007%20onewish.js).

Also, your page is completely invalid, the two paragraphs overlap in Firefox rendering them unreadable, and you've added something to my function:
function loadScript(url) {
var s = document.createElement("script");
s.type = "text/javascript";
s.src = encodeURI(url);
return document.getElementsByTagName("head")[0].appendChild(s);

numbers();winners();date();
}This code will never actually be executed, since it's after a return statement, and would cause an error even if it did, since the script loads asynchronously and hasn't finished at that point.

martyn1987
07-18-2007, 12:13 PM
ah ha

i had'nt thoguht of that, thanks for that

i have added a button to it and now the code works beaufifully

thanks for yours help

most appriciated


XD

martyn1987
07-18-2007, 02:01 PM
heya i have uploaded this file and now the code doesnt want to work , i cant figure out whats wrong with it

it works locally but when i upload it to my server and run it off the site it doesnt work i get an error on line 25


<input id="change" language="javascript" onclick="winners();date();numbers();" style="left: 330px; position: absolute; top: 979px" type="button" value="Click to load numbers" />

this only occurs once i press the button

and help???

thanks in advance