Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For In.... Frustration!!

Status
Not open for further replies.

dalar

Programmer
Sep 19, 2004
18
0
0
GB
Hi guys, been working on this problem for a while now and Im exhausted.

I understand that the 'For In' loop can print out the properties and methods for a given object.

I would like to do this for the 'event' object... Using the code below I am able to extract the properties for the 'navigator' object perfectly:

for (i in navigator) {
document.write("property: " + i);
document.write(" value: " + navigator + "<br>");
}

Now, when I replace navigator with 'event' I get nothing!

for (i in event) {
document.write("property: " + i);
document.write(" value: " + event + "<br>");
}

NOTE: I have also tried window.event

Why is this and what is the correct way to get it working?

thanks for your help guys.
 
the event object does not exist implicitly for all browsers. e.g. in mozilla, you must pass it as an argument to the function that needs it:

Code:
<html>
	<head>
		<title>test</title>
		<script type="text/javascript">
			function showEvent(e) {
				var evt = e ? e : event;
				
				var ta = document.getElementById("output");
				
				for (p in evt) {
					ta.value += p + ":" + evt[p] + "\n";
				}
			}
			
			window.onload = function() {
				window.document.onclick = showEvent;
			}
		</script>
	</head>

	<body>
		<p>click anywhere</p>
		<textarea id="output" style="width:100%; height:30em;"></textarea>
	</body>
</html>

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top