...

View Full Version : One action, many event handlers (again) "resolved"



JamesGreen
04-18-2009, 03:55 PM
Hi everyone,
I guess this may have been a question before but I couldn't search for it because the term "if" was too small...

So I'm a really big noob, started javascript today, looked everywhere and ended up here.

I have this javascript,

if (document.getElementById) { window.onload = swap };
function swap() {
var numimages=2;
rndimg = new Array("./Images/image_01.jpg", "./Images/image_02.jpg");
x=(Math.floor(Math.random()*numimages));
randomimage=(rndimg[x]);
document.getElementById("image_01").style.backgroundImage = "url("+ randomimage +")";
}
if (document.getElementById) { window.onload = swap };
function swap() {
var numimages=2;
rndimg = new Array("./Images/image_03.jpg", "./Images/image_04.jpg");
x=(Math.floor(Math.random()*numimages));
randomimage=(rndimg[x]);
document.getElementById("image_02").style.backgroundImage = "url("+ randomimage +")";
}

But the problem is, the second if gets called the last in the html and so the first one isn't used when displayed (Hope I can explain it clearly)

So how can I get rid of the "if" things?

Oh BTW, the lines who uses javascript in my html file are

<a href="./iSoftware.html" id="image_01"></a>
<a href="./Contribute.html" id="image_02"></a>

and CSS:

#image_01 {
position: absolute;
background-image:url("../Images/image_01.png);
top:105;
left:52;
width:249px;
height:150px;
float:left;
}
#image_02 {
position: absolute;
background-image:url("../Images/image_03.png);
top:105;
left:302;
width:249px;
height:150px;
float:left;
}

Thanks

Philip M
04-18-2009, 07:08 PM
You cannot have two functions swap() with the same name as the seond overwrites the first. I think you have taken a script and simply duplicated it to change a different set of images - that will not work unless you rename the second function and all the global variables within it.

Nor may you have multiple window.onload statements as once again the second will over-write the first. For more info see:-

http://www.javascriptkit.com/javatutors/multiplejava.shtml

Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose.

Not sure why you have posted this Javascript question in this JSON forum.


"A little sincerity is a dangerous thing, and a great deal of it is absolutely fatal". - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)

JamesGreen
04-18-2009, 09:43 PM
Not sure why you have posted this Javascript question in this JSON forum.
Thought I needed to be placed in the DOM section, well, now I guess the "JavaScript frameworks" forum. Admin ?:rolleyes:

Thanks for your answer and sorry for all the problems I started. I shouldn't deserve an answer after all I caused...

Sorry:o

Philip M
04-19-2009, 08:01 AM
You can edit the title yourself.

It is nothing to do with Javascript Frameworks. JavaScript Frameworks
forum is for people wanting help with one of the popular JS frameworks, such as Prototype, jQuery, YUI etc. The correct forum here is Javascript Programming. Ask a mod to move you over. They are listed at the bottom of the main threads page.

JamesGreen
04-19-2009, 10:36 AM
You can edit the title yourself.

It is nothing to do with Javascript Frameworks. JavaScript Frameworks
forum is for people wanting help with one of the popular JS frameworks, such as Prototype, jQuery, YUI etc. The correct forum here is Javascript Programming. Ask a mod to move you over. They are listed at the bottom of the main threads page.

Thanks for that, I've changed the title and PM'd a Client side development moderator..

Haven't got my Javascript working but now that I have discovered that it's a very common question, I'll work it out somehow because there's lots of documentation.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum