SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PopulateOnLoad Cross Browser Problem

    Hi

    I have a website http://www.bristolstreet.co.uk/ that has a car search box functionality which uses an external javascript file to populate the makes and models within the dropdown boxes.

    A few weeks ago I realised that it didn't work in safari (initilly I had the populate onload code attached to the submit image), I then moved the code into a inline script tag and it worked but now i've realised it doesn't work in Firefox... I am now thinking of adding a script to determine the browser and dynamically work on adding the populate code depending on the browser but thought it would be a good idea to post on here in case there is an overall much better solution. If you visit the site you will see a working example of the issue.

    Any help would be great.

    Regards
    Rachel

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Where-abouts is veh_srch defined?

    Currently you are using it as if it were a global variable, but it doesn't currently exist.
    I presume that you're wanting to refer to a form instead?

    The name attribute on the form is considered to be an inelegant way of referring to page elements. Long ago when HTML 4 was being created, it was deemed that unique identifiers should be preferred over the name attribute. The name attribute should only be used to identify individual form elements.

    If your form was identified as follows:

    Code html4strict:
    <form id="veh_srch" method="post">

    You could then associate the javascript variable veh_srch with the form by using:

    Code javascript:
    var veh_srch = document.getElementById('veh_srch');

    These script parts need to occur after the form, at a minimum. Preferable is to have the script just before the </body> tag, but it's also acceptable to instead use an onload event.

    Since using inline event attributes is considered to be a big no-no when it comes to associating elements with events, this could also be a good time to associate a function with the onsubmit event of the form.

    Code javascript:
    veh_srch.onsubmit = function () {
        return SubmitSearch(this);
    }

    Since the form is the element that triggers the onsubmit event, the this keyword will refer to the form itself.
    Last edited by paul_wilkins; Oct 11, 2010 at 09:45.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much, you are a star - it now works and I will make it priority to learn best practice and implement the preferred solution in the very near future.

    Rachel


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
  •