Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2007
    Thanked 0 Times in 0 Posts

    problem finding new object when calling function in my Javascrip file

    I posted this request for help on the AJAX forum but now I discovered the problem does not have anything to do with AJAX. I re-post it here:

    I am not a Javascript expert and if possible I would like to have some help with this problem. I have the following simple HTML where I create a new MyForm object.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
    <html lang="en" dir="ltr" >
    <script src="http://lxplus012.cern.ch:1972/temporary/WebLib.js" ></script>
    <link type="text/css" href="http://lxplus012.cern.ch:1972/temporary/style.css" rel="stylesheet"  />
    <meta http-equiv="pragma" content="no-cache"  />
    <script type="text/javascript" >
    var myform = new MyForm('myform');
    <h1 style="font-family: arial" >Elements Status</h1>
    <p id="formsParId" >
    <body onload="printList()" >
    Then I have my WebLib.js javascript where I have several functions: one of them is the printList() function called inthe HTML. The other is makeHttpRequest() which is called when i press a button on my web page. Here a snippet of my .js file:

    function makeHttpRequest() 
      printList(); // I simplified this function (where AJAX is used) to only call printList()
    function MyForm(name){
      function printList(){
            for(var i in MyForm.list){
     	 alert("Found form i="+i);
    My problem is that if I call printList() from my HTML using onload="printList()" then everything is fine and my MyForm.list is not 0 , i.e. I get to the line alert("Found form i="+i);

    If I get rid of the call to printList() in HTML above and I call printList() inside the function makeHttpRequest(), then it does not find anithing in MyForm.list, i.e. I only get to the
    first line alert("printList") in printList(); Does anybody know why? Is there a way to solve this keeping the declaration "var myform = new MyForm('myform');" in the HTML ?
    Can anybody help please?

    Thanks in advance!

  2. #2
    New Coder
    Join Date
    Apr 2007
    Silicon Valley California
    Thanked 0 Times in 0 Posts
    Your variable 'MyForm' is a Function not an Object. It looks like your 'MyForm' function is meant to create a scoped object, but you never set it to anything. You need to initialize your form with a method like:

    var foo = new MyForm(name);
    However, if you don't need all the power of a scoped object, but just need the HTMLFormElement Object, then I suggest you scrap the 'MyForm' function altogether.

    First create a variable 'MyForm' in the global scope:

    var MyForm = null;
    Then in your onload event callback set MyForm equal to your HTMLFormElement Object with:

    var myOnLoad() = function() {
    MyForm = document.getElementById('MyFormId');
    Now any function can use 'MyForm' and will find it has a value. If you don't already have, I suggest you get the 'FireBug' extension for FireFox, which allows you to step through your JavaScript code and see what values variables have (as well as a whole lot more).
    Last edited by cyberlogi; 06-07-2007 at 07:30 PM. Reason: mispelled words


Posting Permissions

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