SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Beer Monster -Ox's Avatar
    Join Date
    Apr 2003
    Location
    sowth afreeka
    Posts
    374
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript functions loading...

    Hi all,

    I was wondering, we are busy with development at the moment, where I have written a validations page, which basically includes a whole bunch of common functions that I use when validating forms, IE one that checks for empty fields, on that checks phone numbers, one that checks valid email addy's etc all using regex's.

    Some of the forms are only 2 or 3 fields, and doesnt require all the functions on that page, mainly only 1 or 2.

    I have a couple of questions / queries
    When I load that validation_functions.js file into the page, should I perhaps create a smaller version of that file and load that on the pages with smaller forms.

    How does the browser read the functions when they are loaded, does it read through each of them and remember them, then use them when needed.

    Currently we are dynamically adding the correct validations for the correct pages, should I just load these validation functions, into the base index template?

    If I load them into the base index template, there are loads of content pages that would not use these fucntions at all, is that ok? does the browser cache the scripts?

    SOrry about the babble, but I am just wondering how all this works, im finally getting the hang of the javascript thing, but just wanted to see how it all works and what to include where for the best performance.

    Thanks
    <Ox />
    My postings are a natural product.
    The slight variations in spelling and grammar enhance its
    individual character and beauty and in no way are to be
    considered flaws or defects - http://www.guinnesspig.net

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,499
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I load that validation_functions.js file into the page, should I perhaps create a smaller version of that file and load that on the pages with smaller forms.
    It is an option but harder to control

    How does the browser read the functions when they are loaded, does it read through each of them and remember them, then use them when needed.
    each function is actioned when called

    Currently we are dynamically adding the correct validations for the correct pages, should I just load these validation functions, into the base index template?
    If the script is an External JavavaScript once in the cashe it is available for use on othe pages, same as an image, will no/limited overheads.

    Looking at using a common verification script
    The use of cutom attributes can be usefull to decide is a set of verification criteria is to be used

    see this very simple example
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    <!--
    
    function Verify(obj){
     eles=obj.elements;
     zxcMess='';
     for (zxc0=0;zxc0<eles.length;zxc0++){
      if (eles[zxc0].getAttribute('zxcckfill')){
       if (eles[zxc0].value==''){
        zxcMess+='Complete '+eles[zxc0].title+'\n';
       }
      }
      if (eles[zxc0].getAttribute('zxcnofill')){
       if (eles[zxc0].value!=''){
        zxcMess+='Do Not Complete '+eles[zxc0].title+'\n';
       }
      }
     }
     if (zxcMess==''){
      return true;
     }
     alert(zxcMess)
     return false;
    }
    
    //-->
    </script></head>
    
    <body>
    <form onsubmit="document.Show.Show1.value= Verify(this); ">
    <input zxcckfill='Y' title="Field 0" value="" type="text" size="10" ><br>
    <input zxcckfill='Y' title="Field 1" value="" type="text" size="10" ><br>
    <input zxcnofill='Y' title="Field 2" value="Vic" type="text" size="10" ><br>
    <input type="button" value="Submit" onclick="Verify(this.form);"><br>
    
    
    
    </form>
    </body>
    
    </html>


    useing serverside to only create the require functions is an option
    If I load them into the base index template, there are loads of content pages that would not use these fucntions at all, is that ok? does the browser cache the scripts?

  3. #3
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The whole JS file is parsed into internal tokens by the browser, so it indeed "reads the functions when they are loaded", "reads through each of them and remember them, then use them when needed." However, this whole process is very fast and when it comes to script size, the performance bottleneck is not the parsing but rather the downloading of the script.

  4. #4
    Beer Monster -Ox's Avatar
    Join Date
    Apr 2003
    Location
    sowth afreeka
    Posts
    374
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the input guys,

    so basically, I should try and load the fucntions into the main index page, (they are an external file) and then once it loaded, its loaded, and can be used again where it needs (or) does it download the javascript every time the page laods?!

    THanks again for the help
    -Ox
    My postings are a natural product.
    The slight variations in spelling and grammar enhance its
    individual character and beauty and in no way are to be
    considered flaws or defects - http://www.guinnesspig.net

  5. #5
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depends on the browser cache settings. By default, most browsers will try to cache everything possible, including script files. It also depends on your server config: don't send the script with a HTTP header expire set to a date already past, which means it won't be cached at all (if the browser respects that header). Default server configs usually don't do this, so you probably don't have to worry about it.

  6. #6
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Yep

    Well,

    I'd definitely use JavaScript to validate forms, I'd use this book as reference (mentioned it already in a different post), and of course apply it on IE and firefox, and we shouldn't forget Safari of course, hah.

    Jason


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
  •