A beginner's question: "document.form has no properties": what am I missing?

61 views
Skip to first unread message

Jonathan Orlev

unread,
Jan 30, 2008, 6:53:36 AM1/30/08
to greasemonkey-users
Hello everybody,

This is the first time I am trying to create a script for
Greasemonkey.

I am trying to something very simple that I need, but it is not
working.

I am absolutely sure that this is a simple problem, but I don't know
where it is.

So I a created a test scenario to better show where the problem is.

Here is my HTML file in which the script should run on:
===================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Test </TITLE>
</HEAD>

<BODY>
<form name="form1">
<input type="text" name="text1" value="AAA">
</form>
</BODY>
</HTML>
===================

And here is the Greasemonkey script:
===================


// Test01.user.js
//
// ==UserScript==
// @name Test01.user.js
// @namespace file:///D:/mystaff/MySRC/MyHTML/myJavaScript/MyGreasemonkeyScripts/Tests/Test01.user.js
// @description Test
// @include file:///D:/mystaff/MySRC/MyHTML/myJavaScript/MyGreasemonkeyScripts/Tests/Test01.html
// ==/UserScript==


alert(document.form1.elements.text1.value);

===================

Now, when opening the html file I expect to get an alert dialog which
shows "AAA" (which is the value in the text field), but instead I get
nothing, and in the error messages console I can see this error:
"
Error: document.form1 has no properties
Source File: file:///D:/mystaff/MyFireFoxFiles/MyFireFoxProfileAtWORK/gm_scripts/test01userjs.user.js
Line: 12
"

Which obviously refers to this line in the Greasemonkey script:
alert(document.form1.elements.text1.value);

What am I missing here?

ThanQ very much for any reply,

Jonathan

Jonathan Orlev

unread,
Jan 30, 2008, 7:11:00 AM1/30/08
to greasemonkey-users
A small remark:

the 'alert' line should read:
alert(document.form1.text1.value);

and not
alert(document.form1.elements.text1.value);

(I forgot to delete the "elements" property), but it does not change
the situation.

Jonathan

无事

unread,
Jan 30, 2008, 8:19:07 AM1/30/08
to greasemonkey-users
Your way is OK in IE, but can't work in firefox.
You need to use id instead of name,Try this way.

<form name="form1">
<input type="text" id="text1" name="text1" value="AAA">
</form>

function $id(id,doc)
{
if(!doc) doc = document;
return doc.getElementById(id);
}

alert($id("text1").value);

LouCypher

unread,
Jan 30, 2008, 8:23:40 AM1/30/08
to greasemonkey-users
alert(unsafeWindow.document.form1.text1.value)

Jonathan Orlev

unread,
Jan 30, 2008, 8:43:59 AM1/30/08
to greasemonkey-users
LouCypher,

ThanQ for your answer. This indeed solved the problem.

Regards (and thanks again),

Jonathan

Jonathan Orlev

unread,
Jan 30, 2008, 8:47:31 AM1/30/08
to greasemonkey-users

Hello 无事,

ThanQ for your answer.

But are you sure that you are referring to the Greasemonkey problem?

The problem I referred to is specific to Greasemonkey:

If, after the html page is loaded I type this in the address line:

javascript:alert(document.form1.text1.value);

I do get the expected result.

Thanks again,

Jonathan

无事

unread,
Jan 30, 2008, 9:08:04 AM1/30/08
to greasemonkey-users
I believe *document.form1.text1.value* only work in IE so far. haha.

Using unsafeWindow isn't recommended way in Greasemonkey.

Jonathan Orlev

unread,
Jan 30, 2008, 9:25:26 AM1/30/08
to greasemonkey-users
无事,

This line works for sure with Firefox, I tested it.

But I am also going to try your solution: If it works, than I agree
that it is probably better than unsageWindow (not that I am sure what
it is :-) ).

Thanks again for the help,

Jonathan

Anthony Lieuallen

unread,
Jan 30, 2008, 9:43:27 AM1/30/08
to greasemon...@googlegroups.com
On 1/30/2008 6:53 AM, Jonathan Orlev wrote:
> Here is my HTML file in which the script should run on:
> ...

> <form name="form1">
> <input type="text" name="text1" value="AAA">
> </form>
> ...

> And here is the Greasemonkey script:
> ...
> alert(document.form1.elements.text1.value);
> ...

Don't use unsafeWindow. UnsafeWindow is bad in a variety of ways, and
it is almost *never* required. Just learn how to use the tools.

http://www.oreillynet.com/lpt/a/6257

Pitfall #3. You can use the method mentioned there, or use xpath to
find "//form['form1'=@name]".

Jonathan Orlev

unread,
Jan 30, 2008, 10:18:22 AM1/30/08
to greasemonkey-users
Thanks Anthony,

I'll check it up.

Thanks,

Jonathan

Jonathan Orlev

unread,
Jan 30, 2008, 10:58:54 AM1/30/08
to greasemonkey-users
Hello Anthony,

Here is the example GM script again, and this time uses both method to
achieve the same result.
The first uses the wrong and unsafe unsafeWindow object, and the
second uses the method you suggested, from the article, in Pitfall #3.

Can you please tell me if the second method is OK? Is this what you
meant?

As for using XPath, I know something about it, but I never used it
before (I am not a professional developer).

====================================


// Test01.user.js
//
// ==UserScript==
// @name Test01.user.js
// @namespace file:///D:/mystaff/MySRC/MyHTML/myJavaScript/MyGreasemonkeyScripts/Tests/Test01.user.js
// @description Test
// @include file:///D:/mystaff/MySRC/MyHTML/myJavaScript/MyGreasemonkeyScripts/Tests/Test01.html
// ==/UserScript==


//alert(document.form1.elements.text1.value);


// What I want to achieve:
// To show an alert box which specify the content for the text field
named
// "text1", which is an element of the (only) form in the page named
"form1".

// Method 1:
alert( unsafeWindow.document.form1.text1.value );

// Method 2:
var arFormsInputFields= document.getElementsByTagName('input');

for (var i = arFormsInputFields.length - 1; i >= 0; i--)
{
var elmCurFormInputField = arFormsInputFields[i];

if (elmCurFormInputField.name == "text1")
{
alert(elmCurFormInputField.value);
}

}

====================================

Thanks for all the good help,

Jonathan

On Jan 30, 4:43 pm, Anthony Lieuallen <arant...@gmail.com> wrote:

Anthony Lieuallen

unread,
Jan 30, 2008, 11:02:53 AM1/30/08
to greasemon...@googlegroups.com
On 1/30/2008 10:58 AM, Jonathan Orlev wrote:
> Can you please tell me if the second method is OK? Is this what you
> meant?
>
> // Method 2:
> var arFormsInputFields= document.getElementsByTagName('input');

This is not what I meant. The code posted under pitfall#3 uses
"namedItem". If you really want a handle to the input, this should work
too, but it seems a bit verbose.

Jonathan Orlev

unread,
Jan 30, 2008, 11:20:42 AM1/30/08
to greasemonkey-users
And a third (and last?) method to do the same:

alert(document.getElementById('text1').value);

I think this one is both short _and_ safe.

Do you agree with me? should I use this one?

Thanks again,

Jonathan

Jonathan Orlev

unread,
Jan 30, 2008, 11:23:18 AM1/30/08
to greasemonkey-users
无事,

I now think I see what you meant. See my last reply to Anthony.

Thanks,

Jonathan

On Jan 30, 3:19 pm, "无事" <wushi777.wiz...@gmail.com> wrote:

Anthony Lieuallen

unread,
Jan 30, 2008, 11:23:47 AM1/30/08
to greasemon...@googlegroups.com
On 1/30/2008 11:20 AM, Jonathan Orlev wrote:
> And a third (and last?) method to do the same:
> alert(document.getElementById('text1').value);
> I think this one is both short _and_ safe.
> Do you agree with me? should I use this one?

Only if the element actually _has_ an ID of "text1". If it does then
yes, this is the most straightforward and universally good and sane way
to get a handle to the element. That's the point of the ID attribute.

Note that Windows Crapsplorer will return elements with _name_ of
"text1" if no element with _ID_ of "text1" exists.
Mozilla/Opera/Safari/anything decent does not. So you really need the
ID, not the name.

Jonathan Orlev

unread,
Jan 30, 2008, 11:55:09 AM1/30/08
to greasemonkey-users

Yes, there is an ID and (a probably redundant) 'name' tags there, so
this is what I'll use.

Thanks, you helped a lot. :-)

Jonathan

Jonathan Orlev

unread,
Jan 30, 2008, 11:56:40 AM1/30/08
to greasemonkey-users
tags --> attributes.
Reply all
Reply to author
Forward
0 new messages