Null or not an object - Not displaying document.write(...) in child modal window .

G

Gretjns

I'm trying to figure out this script doesn't display any text in the
child window and why I'm getting the null or not an object error.
It's taken directly from the Javascript and DHTML cookbook (not listed
in the book errata on o'reilly website). Initially I get an error
"window.dialogArguments.yourName" is null or not an object. Then I
fill out the field on the form, press the button and the child window
does display but there is no text inside the child window.

Questions: Should I be declaring an object that isn't currently
declared like "window"? or is "window" a built in object that doesn't
need declaring? do I need to assign the dialogDoc.html or "result" to
"document" somehow? It looks like they have me putting a value in
"result" then never actually using "result"... confused... Using ie
6.02800...Suggestions? gj


<html>
<head>
<title> Launch a Modal Dialog</title>
<script type="text/javascript">

function openDialog(form){
var result = window.showModalDialog("dialogDoc.html", form,
"dialogWidth:300px; dialogHeight:201px; center:yes");
}
</script>
</head>
<body>
<h1>Internet Explorer Modal Dialog Window</h1>
<hr />
<form name="sample" action="#" onsubmit="return false">
Enter your name for the dialog box:<input name="yourName" type="text"
/>
<input type="button" value="Send to Dialog"
onclick="openDialog(this.form)" />
</form>
</body>
</html>

<html>
<head>
<title>Modal Dialog</title>
</head>
<body>
<script type="text/javascript">
document.write("Greetings from " +
window.dialogArguments.yourName.value + "!");
</script>
</body>
</html>
 
M

Michael Winter

The "this.form" on the onclick event of the button looks kind of
out-of-place to me, I typically use a different way to reference
objects. [...]

In intrinsic events, the this operator refers to the element. So with

<input ... onchange="this....">

the object reference yielded by the this operator refers to that INPUT
element. Furthermore, all form controls contain a property, form, which
refers to the containing form, if one exists.
document.forms("sample")

You should use square brackets to subscript collections. The use of
parentheses appears to be a Microsoft invention that isn't supported by
all browsers. It certainly fails in the Mozilla Suite and Firefox.

document.forms['formName']

[snip]

Mike
 
R

Richard Cornford

Jc said:
The "this.form" on the onclick event of the button looks kind of
out-of-place to me,

A function assigned as the event handler for an element is executed as a
method of that element and as a result the - this - keyword will refer
to that element. The - form - property of form controls is a reference
to the containing form (or null if the control is outside of the form).
The property was implemented in the earliest browsers that implemented
forms and has been formalised in the W3C HTML DOM standard, making -
this.form - the most reliable (and portable) way of passing on an
anonymous reference to the containing form from a form control event
handler fucntion.
I typically use a different way to reference
objects. Here's something else to try instead of "this.form":

document.forms("sample")
<snip>

Parenthesising "sample" turns a property accessor into a method call. It
is a Microsoftism that, while supported by some other browsers, is not
universally supported. It is also contrary to the W3C HTML DOM
specification, where the ECMAScript bindings explicitly dictate the
actions of dot/bracket notation accessors, but do not specify that the -
forms - collection should act as a function.

A standard bracket notation property accessor:-

document.forms["sample"]

- Works 100% on IE, and every other browser implementing
-document.forms - collection. It is the cross-browser formulation to
use in this context (the Microsoft parenthesised version should never be
used, least it becomes a bed habit that makes cross-browser scripting
seem harder than it really is).

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top