Topic: Cross browser addEventListener function (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=31837" title="Pages that link to Topic: Cross browser addEventListener function (Page 1 of 1)" rel="nofollow" >Topic: Cross browser addEventListener function <span class="small">(Page 1 of 1)</span>\

 
Hustluz
Bipolar (III) Inmate

From:
Insane since: Jun 2003

IP logged posted posted 05-28-2010 20:43 Edit Quote

I am working from a tutorial trying to write a cross browser addEventListener function. I see that it works in firefox and IE but not so good in safari. Can anyone tell me how to fix my code so that it works with safari?

code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Simple Event Example</title>
<script type="text/javascript">
function addEventHandler(oNode, evt, oFunc, bCaptures)
{
	if (typeof(window.event) != "undefined")
		oNode.attachEvent("on"+evt, oFunc);
	else
		oNode.addEventListener(evt, oFunc, bCaptures);
}

function onLinkClicked(e) {
    alert("You clicked the link!");
}

function setUpClickEvent(e) {
    addEventHandler(document.getElementById("clickLink"), "click", onLinkClicked, false);
}

addEventHandler(window, "load", setUpClickEvent, false);
</script>
</head>
<body>
<a href="#" title="click me" id="clickLink">Click Me!</a>
</body>
</html>



thanks again in advance.

oh by the way im using safari on a macbook pro with osx 10.6

(Edited by Hustluz on 05-28-2010 20:44)

BillyRayPreachersSon
Bipolar (III) Inmate

From: London
Insane since: Jul 2004

IP logged posted posted 08-05-2010 21:26 Edit Quote

Instead of testing for window.event, you are better off testing for the presence of the calls you wish to use:

code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
	<title>Simple Event Example</title>

	<script type="text/javascript">

		function addEventHandler(oNode, evt, oFunc, bCaptures) {
			if (document.addEventListener) {
				// Safari, Chrome, Fx, etc
				oNode.addEventListener(evt, oFunc, bCaptures);
			} else if (document.attachEvent) {
				// IE
				oNode.attachEvent('on' + evt, oFunc);
			} else {
				// If all else fails
				oNode['on' + evt] = oFunc;
			}
		}

		function onLinkClicked(e) {
			alert("You clicked the link!");
		}

		function setUpClickEvent(e) {
			addEventHandler(document.getElementById('clickLink'), 'click', onLinkClicked, false);
		}

		addEventHandler(window, 'load', setUpClickEvent, false);
	</script>
</head>

<body>
	<a href="#" title="click me" id="clickLink">Click Me!</a>
</body>
</html>



Dan
\

coach
Nervous Wreck (II) Inmate

From:
Insane since: May 2011

IP logged posted posted 05-31-2011 11:00 Edit Quote
Edit TP: spam removed


Post Reply
 
Your User Name:
Your Password:
Login Options: Remember Me On This Computer
 
Your Text:
Loading...
Options: Show Signature
Enable Slimies
Enable Linkwords

« BackwardsOnwards »

Show Forum Drop Down Menu