Why does this alert say there aren't any forms?

T

taras.di

Hi everyone,

I've just spent the last 2 hours banging me head against the desk
trying to figure this one out. I eventually figured out that sometimes
a form doesn't exist for the popup I'm specifying. I'm trying to write
a few functions that help me debug my script by writing variables to a
text area in a popup window. I came up with the following code:

var debug;
var debugWindow;

function setDebug(debugLevel)
{
debug = debugLevel;
}

function setupDebug(debugLevel, testURL)
{
if (debug != "none")
{
// set up a debug window
debugWindow =
window.open(testURL, "debugWindow", "menubar=no, status=no,"+
"width=400, height=300, toolbar=no, directories=no");
}
}

function writeDebug(message)
{
if (debug != "none")
{
var oldValue = debugWindow.document.forms[0].elements[0].value;
var newValue = oldValue + "\n" + message;
debugWindow.document.forms[0].elements[0].value = newValue;
}
}
setupDebug("debug","http://localhost:8080/debugArea.htm");
alert("FORMS: "+debugWindow.document.forms.length);
debugWindow.document.theForm.debugArea.value = "hello everyone";

debugArea.htm contains the following:

<HTML>
<BODY>
<FORM NAME ="theForm" METHOD =post>
<TEXTAREA ROWS ="15" COLS ="40" NAME ="debugArea">
</TEXTAREA>
</FORM>
</BODY>
</HTML>

The first time I load the script, the window pops up, and the alert
informs me the number of forms on the page is zero. Subsequent
refreshes (where the popup-window is already present) show that the
number of forms is 1.

Also, why doesn't 'hello everyone' ever popup.

Thanks

Taras
 
B

bwucke

(e-mail address removed) napisal(a):
Hi everyone,

I've just spent the last 2 hours banging me head against the desk
trying to figure this one out. I eventually figured out that sometimes
a form doesn't exist for the popup I'm specifying. I'm trying to write
a few functions that help me debug my script by writing variables to a
text area in a popup window.

Why not use venkman instead?
 
P

Patient Guy

(e-mail address removed) wrote in @g47g2000cwa.googlegroups.com:
Hi everyone,

I've just spent the last 2 hours banging me head against the desk
trying to figure this one out. I eventually figured out that sometimes
a form doesn't exist for the popup I'm specifying. I'm trying to write
a few functions that help me debug my script by writing variables to a
text area in a popup window. I came up with the following code:

var debug;
var debugWindow;

function setDebug(debugLevel)
{
debug = debugLevel;
}

function setupDebug(debugLevel, testURL)
{
if (debug != "none")
{
// set up a debug window
debugWindow =
window.open(testURL, "debugWindow", "menubar=no, status=no,"+
"width=400, height=300, toolbar=no, directories=no");
}
}

function writeDebug(message)
{
if (debug != "none")
{
var oldValue = debugWindow.document.forms[0].elements[0].value;
var newValue = oldValue + "\n" + message;
debugWindow.document.forms[0].elements[0].value = newValue;
}
}
setupDebug("debug","http://localhost:8080/debugArea.htm");
alert("FORMS: "+debugWindow.document.forms.length);
debugWindow.document.theForm.debugArea.value = "hello everyone";

debugArea.htm contains the following:

<HTML>
<BODY>
<FORM NAME ="theForm" METHOD =post>
<TEXTAREA ROWS ="15" COLS ="40" NAME ="debugArea">
</TEXTAREA>
</FORM>
</BODY>
</HTML>

The first time I load the script, the window pops up, and the alert
informs me the number of forms on the page is zero. Subsequent
refreshes (where the popup-window is already present) show that the
number of forms is 1.

Also, why doesn't 'hello everyone' ever popup

You have three lines of script at the document level (not defined in a
function, so-called 'anonymous script').

Where you place them affects how they will respond to execution.

Probably you want to place them within a script element (between script
start and end tags) at the end of your HTML document, just before the BODY
etago. Your script variables/object.properties can only reference data to
which they are assigned once they are defined. Definition of document
data/objects only occurs when the document reader (rendering engine)
actually parses the content of the document. Thus if you reference a form
in script BEFORE the HTML document renderer has seen the form element,
then you will generate a script error (which is why you noticed that the
form "doesn't exist"). If you have set your browser to inform you of
script errors/exceptions, you probably would have made this determination
in a shorter time.
 
L

Lasse Reichstein Nielsen

function setupDebug(debugLevel, testURL)
{ .... opens a window...
} ....
setupDebug("debug","http://localhost:8080/debugArea.htm");
alert("FORMS: "+debugWindow.document.forms.length);
debugWindow.document.theForm.debugArea.value = "hello everyone";

Here you try to write to the window immediately after opening it.
Opening a window is handled asynchroneously, so you can't expect the
window to have loaded its page yet. You should wait a little before
trying to access the window's contents, giving it time to load.

You should also check whether the page is loaded from the same domain
as the page using it. If not, cross domain scripting restrictions may
interfere with your accessing the page.


/L
 
T

Thomas 'PointedEars' Lahn

var debug;
var debugWindow;

function setDebug(debugLevel)
{
debug = debugLevel;
}

It really does not make sense to implement a method that will simply assign
a passed value to a global variable. If you wanted to use encapsulation,
learn about the underlying object model first.

function setupDebug(debugLevel, testURL)
{
if (debug != "none")
{
// set up a debug window
debugWindow =
window.open(testURL, "debugWindow", "menubar=no, status=no,"+
"width=400, height=300, toolbar=no, directories=no");

Since you appear to call setupDebug() unconditionally and without real
user interaction below, it is likely that this popup window is blocked.

Besides, the features string (third argument) for window.open() must not
contain whitespace, and most of the features set here are the default.
}
}

function writeDebug(message)
{
if (debug != "none")
{
var oldValue = debugWindow.document.forms[0].elements[0].value;
var newValue = oldValue + "\n" + message;
debugWindow.document.forms[0].elements[0].value = newValue;

Replace those three statements with

debugWindow.document.forms[0].elements[0].value += "\n" + message;

And if you followed my suggestion below, that would be

document.forms[0].elements[0].value += "\n" + message;
}
}
setupDebug("debug","http://localhost:8080/debugArea.htm");
alert("FORMS: "+debugWindow.document.forms.length);
debugWindow.document.theForm.debugArea.value = "hello everyone";

You need to make sure that `debugWindow' aso refers to an object before
you can access its properties and properties of the objects they refer to.

<URL:http://pointedears.de/scripts/test/whatami#inference>

That would be best achieved if you executed code referring to the document
content when the `load' event of its `body' element fires, using its
intrinsic `onload' event handler, and moving the debug code into the
respective document:
debugArea.htm contains the following:

<HTML>
<BODY>

This is not Valid HTML. said:
[...]
The first time I load the script, the window pops up, and the alert
informs me the number of forms on the page is zero. Subsequent
refreshes (where the popup-window is already present) show that the
number of forms is 1.

Yes, because the document in the popup needs time to be fully loaded.
Also, why doesn't 'hello everyone' ever popup.

You try to assign a value to it when you open the popup. But as you
wrote, at that moment there are no forms yet, so

debugWindow.document.theForm.debugArea.value
-------------------------------^
which should have been written as

debugWindow.document.forms['theForm'].elements['debugArea'].value
----------------------------------------^
is a ReferenceError anyway.

<URL:http://jibbering.com/faq/#FAQ4_43>


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Here you try to write to the window immediately after opening it.
Opening a window is handled asynchroneously,

No. Loading the document in a new window is handled asynchronously
regarding window.open().
so you can't expect the window to have loaded its page yet. [...]

s/page/document/

Yes, indeed.


PointedEars
 
T

taras.di

Thanks everyone, that helped a lot. The code written above was destined
to go into a separate file, thus the reason why I had the function
setDebug doing what looked to be useless things. I could use a class I
suppose to encapsulate the data.

The reasons you gave as to why the form isn't seen the first time makes
sense (stupid me, why didn't I think of that) but it doesn't make sense
why the form is found on subsequent reloads. Wouldn't the same problem
occur.

Thanks a lot!!

Taras
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top