473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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

Dec 27 '05 #1
6 1530

ta******@gmail.com 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?

Dec 27 '05 #2
ta******@gmail.com wrote in news:1135660065.843724.282390
@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.
Thanks

Taras


Dec 27 '05 #3
ta******@gmail.com writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Dec 27 '05 #4
ta******@gmail.com wrote:
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.

<URL:http://jibbering.com/faq/>
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. <URL:http://validator.w3.org/>
[...]
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
Dec 27 '05 #5
Lasse Reichstein Nielsen wrote:
ta******@gmail.com writes:
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,


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
Dec 27 '05 #6
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

Dec 30 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Fendi Baba | last post by:
The function is called from opencalendar(targetfield). Thanks for any hints on what could be the problem. .............................................................. var...
2
by: Gayathri | last post by:
Please find the pasted html, <html> <script language="JavaScript" src="cal.js"></script><!-- Date only with year scrolling --> </head> <BODY onLoad="showDetails()"> <script...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
1
by: kamleshsharmadts | last post by:
I am using Ajax with struts in web application. from jsp i am calling a function of ajax.js onclick of a button. code of that call function which calling from jsp given as below:- ...
4
by: yawnmoth | last post by:
I'm trying to display a popup showing which radio button is selected and am unable to do so. Every time I try, I get undefined, instead of the value of the particular form variable I'm trying to...
7
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. ...
3
Ajm113
by: Ajm113 | last post by:
Ok for some reason after the update of Firefox it seems that my form keeps getting submitted when the user made a error on the form. I just want the page to reload so the user may try again. Because...
29
by: zalek | last post by:
I am writing application with Ajax in sync mode - xmlHttp.open("GET", url, false). I noticed that in FireFox handler doesn't starts. It starts when I use xmlHttp.open("GET", url,true). I need to...
8
by: knkk | last post by:
Instead of an id getting its innerHTML changed, the entire page is getting refreshed with this function of mine (you may want to look just at the end of the function where there's an alert): ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.