Groups | Blog | Home
all groups > dotnet jscript > april 2006 >

dotnet jscript : Simple JavaScript


Ken
4/19/2006 6:51:02 AM
I am trying to teach myself a little JavaScript. I am able to write simple
JavaScript like the following:
<!-- NavigatorProperties.html -->

<html>

<body>
<h3>Properties of "Navigator"</h3>
<hr>
<script language="JavaScript">
for (propertyName in navigator) {
document.write(propertyName + "<br>");
}
</script>
</body>

</html>

However, it occurred to me that after duplicating this code several times
for different objects, I could write something that would involve selecting
an object and then getting its report. I made it throu the following code and
am stuck at the comment.

<!-- SelectObjectProperties.html -->

<html>

<head>
<script type="text/javascript" language="javascript"><!--
function writeProperty(obj) { <br><h3>Properties of "obj"</h3><hr>
for (propertyName in obj) { document.write(propertyName + "<br>"); }
}
// -->
</script>
<noscript><p>A Javascript-enabled browser is recommended.</p></noscript>
</head>

<body><h3>Select an Object</h3>

<form name="objects" method="post"><select name="object">
<option value="Navigator">Navigator
<option value="Window">Window
<option value="History">History
<option value="Document" selected>Document
<option value="Location">Location
<option value="Document.Links">Document.Links
<option value="Document.Anchor">Document.Anchor
<option value="Document.Forms">Document.Forms
</select></form>

// -- It seems that something should go here to invoke the writeProperties
function. But I am not sure what should go here. //

</body>

--
Thank you in advance for your time.

Regards,
Josh Twist
4/20/2006 12:46:24 AM
Hi Ken,

I've touched up your example:

<html>


<head>
<script type="text/javascript" language="javascript"><!--
// Note the changes made here:

function writeProperty(obj)
{
document.write('<br><h3>Properties of "obj"</h3><hr>');
for (propertyName in obj)
{
document.write(propertyName + " = " + obj[propertyName] +
"<br>");
}
}
// -->
</script>
<noscript><p>A Javascript-enabled browser is
recommended.</p></noscript>
</head>


<body><h3>Select an Object</h3>


<form name="objects" method="post">
<select name="object" id="mySelect">
<option value="Navigator">Navigator
<option value="Window">Window
<option value="History">History
<option value="Document" selected>Document
<option value="Location">Location
<option value="Document.Links">Document.Links
<option value="Document.Anchor">Document.Anchor
<option value="Document.Forms">Document.Forms
</select></form>

<script type="text/javascript">
writeProperty(document.getElementById('myselect'));
</script>


</body>

That should get you going - enjoy!

Josh
http://www.thejoyofcode.com/
AddThis Social Bookmark Button