Hi,
I’m a javascript novice and really would appreciate some help.
I’m trying to run two javascripts on a page – both are linked externally and use the window.onload command. One is a random image picker and the other is a style switcher. They are llinked like so;
[COLOR=DarkRed]<script language=”JavaScript1.2″ src=”../styles/random.js” type=”text/javascript”></script>
<script language=”JavaScript1.2″ src=”../switcher.js” type=”text/javascript”></script>
However, as I’m sure you’re aware, only the last linked one works.
I’ve tried creating a new function and commenting out the window.onload commands in the scripts so:
[COLOR=Purple]<script language=”JavaScript” type=”text/JavaScript”>
function pageInit(){
swap();
function();
}
window.onload = pageInit;
</script>
but doesn’t seem to work – am I doing something wrong?
How do I make them both work at the same time please?
Scripts are:
[COLOR=Green]if (document.getElementById) { window.onload = swap } ;
function swap() {
var numimages=3;
rndimg = new Array(“images/tops/top1.jpg”, “images/tops/top2.jpg”, “images/tops/top3.jpg””);
x=(Math.floor(Math.random()*numimages));
randomimage=(rndimg[x]);
document.getElementById(“top1”).style.backgroundImage = “url(“+ randomimage +”)”;
}
and
[COLOR=Green]//<![CDATA[
// script (c) copyright [url]http://aleto.ch
// This work is licensed under the Creative Commons Attribution License. To view a
// copy of this license, visit [url]http://creativecommons.org/licenses/by/1.0/
// a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305,
// USA.
function newEle(type, content, toNode) {
var ele=document.createElement(type), i;
for(i=3; i<arguments.length; i+=2) ele[arguments[i]]=arguments[i+1];
if(content) ele.appendChild(document.createTextNode(content));
if(toNode) toNode.appendChild(ele);
return ele;
}
function setStyle(title){
var linkArr=document.getElementsByTagName(‘link’), i, link;
for(i=0; link=linkArr[i]; i++) {
if(/alt.*style/i.test(link.getAttribute(‘rel’))) {
link.disabled=true;
if(title==link.getAttribute(‘title’)) link.disabled=false;
}
}
createCookie(‘currStyle’,title,(360*60
}
function createCookie(name, value, time) {
document.cookie = name+”=”+escape(value)+
“; expires=”+(new Date(new Date().getTime()+time)).toGMTString()+”; path=/”;
}
function readCookie(name) {
if( new RegExp(name+’=([^;]*);’,”).test(document.cookie+’;’) ) return unescape(RegExp.$1);
return null;
}
window.onload=function(){
var linkArr=document.getElementsByTagName(‘link’), link, i, theme=[], title, list, option;
if(title=readCookie(‘currStyle’)) setStyle(title);
for(i=0; link=linkArr[i]; i++)
if(/alt.*style/i.test(link.getAttribute(‘rel’))) theme.push(link);
if(theme.length>0) {
list=newEle(‘select’,”,document.getElementById(‘control’),’onchange’, function(){setStyle(this.value)});
newEle(‘option’, ‘Standard Style’, list);
for(i=0;i<theme.length;i++) {
option=newEle(‘option’, theme[i].title, list,’value’,theme[i].title);
if(option.value==title) option.selected=true;
}
}
}
//]]>
Your help would be very much appreciated!!