onClick event problem

M

Magician

Hello.
I am trying to set the onclick event for images through a function, but the
event is triggered as soon the page loads, then will not work when the image
is clicked. Can anyone suggest what is wrong?
It is happening in both IE6 and Firefox
---------------------------------------------------------------------------
window.onload = fnNewWindowLinks;

function fnNewWindowLinks() {
for (var intLinks=0; intLinks<document.links.length; intLinks++) {
if (document.links[intLinks].className == "picture") {
document.links[intLinks].onClick = alert("Clicked");
}
}
}
 
R

Randy Webb

Magician said the following on 12/25/2006 8:18 PM:
Hello.
I am trying to set the onclick event for images through a function, but the
event is triggered as soon the page loads, then will not work when the image
is clicked. Can anyone suggest what is wrong?
It is happening in both IE6 and Firefox
---------------------------------------------------------------------------
window.onload = fnNewWindowLinks;

function fnNewWindowLinks() {
for (var intLinks=0; intLinks<document.links.length; intLinks++) {
if (document.links[intLinks].className == "picture") {
document.links[intLinks].onClick = alert("Clicked");

Two things:

It is onclick in JS, not onClick
document.links[intLinks].onclick= new Function('alert("Clicked")')
 
M

Magician

Randy Webb said:
Magician said the following on 12/25/2006 8:18 PM:
Hello.
I am trying to set the onclick event for images through a function, but
the
event is triggered as soon the page loads, then will not work when the
image
is clicked. Can anyone suggest what is wrong?
It is happening in both IE6 and Firefox
---------------------------------------------------------------------------
window.onload = fnNewWindowLinks;

function fnNewWindowLinks() {
for (var intLinks=0; intLinks<document.links.length; intLinks++) {
if (document.links[intLinks].className == "picture") {
document.links[intLinks].onClick = alert("Clicked");

Two things:

It is onclick in JS, not onClick
document.links[intLinks].onclick= new Function('alert("Clicked")')

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/


Thanks Randy, I got that working, but when i try to extend it a bit further
to open an image in a window, it is going back to loading the window when
the page loads and not on the click event.
I am following examples is a js book and trying to adapt them for my
purposes but I can't get it working.
Is my problem now the call to the second function?

----------------------
window.onload = fnNewWindowLinks;

function fnNewWindowLinks() {
for (var intLinks=0; intLinks<document.links.length; intLinks++) {
if (document.links[intLinks].className == "picture") {
document.links[intLinks].onclick = fnNewWindow() ;
}
}
}

function fnNewWindow() {
var strImage = "creative/artwork/" + this.id + ".jpg";
var objWindow = window.open(strImage, "PictureWindow","resizable=yes");
return false;
}
 
M

Magician

Thanks but I worked it out after 3 hours of trial and error. just leave the
() off the function call and it works!
Thanks anyway :)

mag



Magician said:
Randy Webb said:
Magician said the following on 12/25/2006 8:18 PM:
Hello.
I am trying to set the onclick event for images through a function, but
the
event is triggered as soon the page loads, then will not work when the
image
is clicked. Can anyone suggest what is wrong?
It is happening in both IE6 and Firefox
---------------------------------------------------------------------------
window.onload = fnNewWindowLinks;

function fnNewWindowLinks() {
for (var intLinks=0; intLinks<document.links.length; intLinks++) {
if (document.links[intLinks].className == "picture") {
document.links[intLinks].onClick = alert("Clicked");

Two things:

It is onclick in JS, not onClick
document.links[intLinks].onclick= new Function('alert("Clicked")')

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/


Thanks Randy, I got that working, but when i try to extend it a bit
further to open an image in a window, it is going back to loading the
window when the page loads and not on the click event.
I am following examples is a js book and trying to adapt them for my
purposes but I can't get it working.
Is my problem now the call to the second function?

----------------------
window.onload = fnNewWindowLinks;

function fnNewWindowLinks() {
for (var intLinks=0; intLinks<document.links.length; intLinks++) {
if (document.links[intLinks].className == "picture") {
document.links[intLinks].onclick = fnNewWindow() ;
}
}
}

function fnNewWindow() {
var strImage = "creative/artwork/" + this.id + ".jpg";
var objWindow = window.open(strImage, "PictureWindow","resizable=yes");
return false;
}
 
A

ASM

Magician a écrit :
Thanks but I worked it out after 3 hours of trial and error. just leave the
() off the function call and it works!

document.links[intLinks].onclick= function(){
alert('Clicked');
return false;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,771
Messages
2,569,587
Members
45,099
Latest member
AmbrosePri
Top