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

Discussion in 'Javascript' started by taras.di, Dec 27, 2005.

  1. taras.di

    taras.di Guest

    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;
    alert("FORMS: "+debugWindow.document.forms.length);
    debugWindow.document.theForm.debugArea.value = "hello everyone";

    debugArea.htm contains the following:

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

    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.


    taras.di, Dec 27, 2005
    1. Advertisements

  2. taras.di

    bwucke Guest

    Why not use venkman instead?
    bwucke, Dec 27, 2005
    1. Advertisements

  3. taras.di

    Patient Guy Guest

    wrote in @g47g2000cwa.googlegroups.com:
    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.
    Patient Guy, Dec 27, 2005
  4. 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.

    Lasse Reichstein Nielsen, Dec 27, 2005
  5. 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.

    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.
    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;
    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.


    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:
    Yes, because the document in the popup needs time to be fully loaded.
    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

    which should have been written as

    is a ReferenceError anyway.



    Thomas 'PointedEars' Lahn, Dec 27, 2005
  6. No. Loading the document in a new window is handled asynchronously
    regarding window.open().

    Yes, indeed.

    Thomas 'PointedEars' Lahn, Dec 27, 2005
  7. taras.di

    taras.di Guest

    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

    Thanks a lot!!

    taras.di, Dec 30, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.