Forum Moderators: open

Message Too Old, No Replies

Script only works in IE!

         

b_b_b

2:18 pm on Oct 26, 2004 (gmt 0)



I'm no expert, but this code only works in Exploiter. I'm using Firefox, please can anyone help?

It has something to do with the "window.event.srcElement;" routine.

<script language="JavaScript">
<!-- //start hiding for older browsers
//create expand menu for previous polls

var img1 = new Image();
//plus image
img1.src = "img/plus.gif";
var img2 = new Image();
//minus image
img2.src = "img/minus.gif";

//create expand menu
function doOutline() {
var srcId, srcElement, targetElement;
srcElement = window.event.srcElement;
if (srcElement.className.toUpperCase() == "LEVEL1" ¦¦ srcElement.className.toUpperCase() == "FAQ") {
srcID = srcElement.id.substr(0, srcElement.id.length-1);
targetElement = document.all(srcID + "s");
srcElement = document.all(srcID + "i");

if (targetElement.style.display == "none") {
targetElement.style.display = "";
if (srcElement.className == "LEVEL1") srcElement.src = img2.src;
} else {
targetElement.style.display = "none";
if (srcElement.className == "LEVEL1") srcElement.src = img1.src;
}
}
}

document.onclick = doOutline;
-->
</script>

RonPK

4:43 pm on Oct 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there b_b_b, welcome to WebmasterWorld.

You're right about window.event.srcElement being IE-only. So instead of using

srcElement = window.event.srcElement;

try something like

function doOutline(e) { // note the e!  
var srcId, srcElement, targetElement;
if (window.event) e = window.event;
srcElement = e.srcElement? e.srcElement : e.target;
// etc.

Next problem you may run into is document.all, which is an IE-only collection. Unless you care about IE4, it's perfectly safe to switch to using document.getElementById(), which is supported by IE5 and up, and by proper browsers such as Mozilla (inlcuding Firefox) and Opera.

kj6loh

8:35 pm on Dec 5, 2004 (gmt 0)

10+ Year Member



So RonPK why will this not work?

<FORM>
<INPUT TYPE="button" VALUE="a button" ONCLICK="
var ctrlKey = document.layers? event.modifiers & Event.CONTROL_MASK : event.ctrlKey;
var shftKey = document.layers? event.modifiers & Event.CONTROL_MASK : event.shiftKey;
if (window.event) e = window.event;
var srcEl = e.srcElement? e.srcElement : e.target;
if (ctrlKey) alert(srcEl.tagName);
if (shftKey) alert(event.type);
if (!(shftKey¦¦ctrlKey)) alert('So why didn\'t you press any keys?');">
</FORM>