PDA

View Full Version : ie acting funny



latemodern
Sep 8th, 2007, 08:41 PM
Hi,

This is probably an obvious mistake but I'm new to javascript. Can anyone spot why this works in Firefox and not ie7? I am trying to add an onclick event handler to all of the anchors in the 'main' div with the 'new_window' class. The alert('hello') works in ie7 but the event handler doesn't. I can't work out whether the event handler insert or the actual function that the event handler is calling is at fault. Here's the 'inserter':



function prepare_OpenWin(){
if(!document.getElementById) return false;
if(!document.getElementsByTagName) return false;
var main = document.getElementById("main")
var links = main.getElementsByTagName("a")
for(var i=0; i < links.length; i++){
if((links[i].getAttribute('class') == 'new_window') || (links[i].getAttribute('className') == 'new_window')){
alert('hello');
var dest = links[i].getAttribute('href');
links[i].setAttribute('onclick', "OpenWin('"+dest+"','430','750'); return false;");
links[i].onkeypress = links[i].onclick;
}

}
}


And here's the function the handler calls:



function OpenWin(Loc,Width,Height) {
var WinInfo = "toolbar=no,scrollbars=yes,directories=no,resizable=yes,menubar=no,width=" + Width + ",height=" + Height
wPageWindow=window.open(Loc,"wImageWindow",WinInfo);
}


Any help would be grand

Thanks!

Kor
Sep 8th, 2007, 09:27 PM
IE has an incomplete implementation of the W3C DOM 1+ recommendations regarding some attributes like class or style. Furthermore, IE does not consider events as attributes.

For all these, DOM 0 syntax is the save:
element.className='someclass';
element.onevent=function(){somefunction(someraguments)}

And you have also some strange unwanted double quotes in your code. And you have a redundant condition

if(condition||samecondition)

Could be something like:


if(links[i].className== 'new_window'){
alert('hello');
var dest = links[i].getAttribute('href');
links[i].onclick=function(){OpenWin(dest,'430','750'); return false}
links[i].onkeypress = function(){OpenWin(dest,'430','750'); return false}
}


I don't see the use for onkeypress, well, anyway...

latemodern
Sep 9th, 2007, 06:03 PM
Thanks a lot! Works beautifully.

The onkeypress is for acessibility - ie. people who can't use a mouse.

Cheers

Chris