/    Sign up×
Community /Pin to ProfileBookmark

Problems combining two scripts with window.onLoad command

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>[/COLOR]

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>[/COLOR]

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 +”)”;
}[/COLOR]
and

[COLOR=Green]//<![CDATA[
// script (c) copyright [url]http://aleto.ch[/url] christian krebs aka aleto 29/11/2004
// 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/[/url] or send
// 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*
24*60*60*1000));
}

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;
}
}
}

//]]>[/COLOR]
Your help would be very much appreciated!!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangDec 06.2005 — window.onload=function() {
swap();
theOther();
}

function theOther() {
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&gt;0) {
list=newEle('select','',document.getElementById('control'),'onchange', function(){setStyle(this.value)});
newEle('option', 'Standard Style', list);
for(i=0;i&lt;theme.length;i++) {
option=newEle('option', theme[i].title, list,'value',theme[i].title);
if(option.value==title) option.selected=true;
}
}
}
Copy linkTweet thisAlerts:
@HarryGreinerauthorDec 08.2005 — Thanks Fang. Worked without a problem and taught me a useful lesson too. Many thanks again.
×

Success!

Help @HarryGreiner spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...