SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Kiwi Fr00t jylyn's Avatar
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Global form variable not working

    I've had this problem on several occasions and I'm not sure what I'm doing wrong.

    I have defined a global variable for a form, but it seems my functions can't access it. The error I get from the Firefox Javascript console tells me that my variable has no properties.

    Code:
    var giftForm = document.forms.uploadGift;
    
    function sendForm() {
      giftForm.target = 'main';
      giftForm.submit();
      window.close();
    }
    Everything runs without a hitch if I declare the form variable inside the function.

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depending on where you are declaring your variable in your script, the most likely explanation is that the form has not been loaded when you try creating your reference to it.

    As an alternative you could try declaring the variable globally, but assign the reference to the form to it in an onload event handler. For example:

    Code:
    var giftForm=null;
    
    window.onload=function(){
    
    giftForm = document.forms.uploadGift;
    } function sendForm() {
    if(giftForm!=null && giftForm!=undefined){
    giftForm.target = 'main'; giftForm.submit(); window.close();
    }
    }

  3. #3
    Kiwi Fr00t jylyn's Avatar
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course that's the reason! Duh. It all makes sense now that you say that.
    Thanks very much!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •