Results 1 to 4 of 4

Thread: How to define a global variable for all the functions

  1. #1
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default How to define a global variable for all the functions

    Hi,

    Here's a sample form:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Sample form</title>
    <script type="text/javascript">
    function displayResult() {
    alert(document.myForm.myInput.value);
    }
    function getFocus() {
      if (document.myForm.myInput.value == document.myForm.myInput.defaultValue) {
        document.myForm.myInput.value = "";
      }
    }
    function loseFocus() {
      if (document.myForm.myInput.value == "") {
        document.myForm.myInput.value = document.myForm.myInput.defaultValue;
      }
    }
    </script>
    </head>
    <body>
    <form name="myForm" method="get" onsubmit="return false;" action="">
    <input name="myInput" value="Hello world!" onfocus="getFocus();" onblur="loseFocus();"><br>
    <input type="button" onclick="displayResult();" value="Display input value">
    </form>
    </body>
    </html>
    It works with no problem, but the following doesn't:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Sample form</title>
    <script type="text/javascript">
    var x = document.myForm.myInput;
    function displayResult() {
    alert(x.value);
    }
    function getFocus() {
      if (x.value == x.defaultValue) {
        x.value = "";
      }
    }
    function loseFocus() {
      if (x.value == "") {
        x.value = x.defaultValue;
      }
    }
    </script>
    </head>
    <body>
    <form name="myForm" method="get" onsubmit="return false;" action="">
    <input name="myInput" value="Hello world!" onfocus="getFocus();" onblur="loseFocus();"><br>
    <input type="button" onclick="displayResult();" value="Display input value">
    </form>
    </body>
    </html>
    What's wrong with it and how can I define a global variable to be used by all the functions?

    Many thanks in advance!
    Mike

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Sample form</title>
    </head>
    <body>
    <form name="myForm" method="get" onsubmit="return false;" action="">
    <input name="myInput" value="Hello world!" onfocus="getFocus();" onblur="loseFocus();"><br>
    <input type="button" onclick="displayResult();" value="Display input value">
    </form>
    <script type="text/javascript">
    var x = document.myForm.myInput;
    function displayResult() {
    alert(x.value);
    }
    function getFocus() {
      if (x.value == x.defaultValue) {
        x.value = "";
      }
    }
    function loseFocus() {
      if (x.value == "") {
        x.value = x.defaultValue;
      }
    }
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Rain Lover (01-17-2012)

  4. #3
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Thumbs up

    Perfect! I just wonder how you compare putting the whole script before the </body> tag and using an onload event, e.g.
    Code:
    var x;
    window.onload = function() {
        x = document.myForm.myInput;
    }

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It's faster than onload. With such a simple page though, the difference is negligible. However, with an average page with a number of images, it can be significant. Or one with tons of images, scripts and videos, it can take a long time before the onload event fires.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •