Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post

    Does putting a "block" of js to run onload within a form element break the js

    I've had a few problems with an ajax script. I've been moving things around to see what works and what doesn't. It is starting to look like placing javascript, within the <form> </form> tags, if the javascript runs onload and posts a value to a php script, breaks the javascript

    Is this true? Does doing this break it?

    Is it best to put the js, that runs onload, somewhere below the closing form tag?

    Thanks
    Last edited by milesdriven; 06-07-2012 at 08:43 PM.

  2. Users who have thanked milesdriven for this post:

    chris77780124 (06-23-2012)

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,459
    Thanks
    0
    Thanked 632 Times in 622 Posts
    There is rarely a need to attach JavaScript to an onload event (the exception being where athe images absolutely must be loaded before the JavaScript that interacts with the images can be allowed to run - a situation that I have never found to actually apply).

    Simply attaching the JavaScript immediately before the </body> tag will allow it to interact with the entire web page without needing onload and without making the script wait for everything else to load.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks for the reply.

    By onload, I mean I put the js between these two lines:
    Code:
    <script type="text/javascript">
    code here
    </script>
    This works to get the browser to run it immediately without placing it inside a function and using an event handler to call it.

    The problem now is there are 2 such scripts (onload) on the same page. Only 1 will run, and that is only when the other is commented out with "//" at the beginning of each line.

    No matter which of these 2 scripts I comment out, the other runs. Both will not run together.

    Firebug tells me the one that will not run has been aborted. What would abort javascript?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,459
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Move the script tags to immediately before the </body> tag.


    Then take whatever is attached to the onload and delete the onload reference from the front.

    For example if you have the following two statements in your two scripts:

    window.onload = abc;
    window.onload = def;

    replace those lines with:

    abc();
    def();
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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