1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

javascript help: onload functions please please please help me.

Discussion in 'XenForo Development Discussions' started by EQnoble, Jun 27, 2011.

  1. EQnoble

    EQnoble Well-Known Member

    I wanted to know if someone knew how to change this function into a script so I can get it out of my html and make my addon not need a template edit to PAGE_CONTAINER

    this is how I am using it now which requires editing a template.
    <body onload="mything()">

    Thanks ahead of time SO MUCH
     
  2. EQnoble

    EQnoble Well-Known Member

    I have solved my problem....thanks everyone anyways. I think I figured out how to solve problems....find the correct definition for what I can't figure out how to do so I can search it :)
    Code:
    <script type="text/javascript">
    function addLoadEvent(func) {
      var oldonload = window.onload;
      if (typeof window.onload != 'function') {
        window.onload = func;
      } else {
        window.onload = function() {
          if (oldonload) {
            oldonload();
          }
          func();
        }
      }
    }
    addLoadEvent(mything);
    </script>
     
  3. Kier

    Kier XenForo Developer Staff Member

    Don't use window.onload if you can possibly avoid it.

    Use the jQuery syntax instead:
    Code:
    $(function()
    {
    // this code will run on DOM ready
    });
     
    EQnoble likes this.
  4. EQnoble

    EQnoble Well-Known Member

    Thanks and good that you caught me... I was just going to release the mod this is for...
    Code:
    $(function()
    {
    addLoadEvent(myThing);
    });
    
    Is that the right usage?....I swapped it with the code i posted above and it does not work...I can send you a link to show you what exactly it is if it helps?

    as you can probably tell I know nothing about js :)
     
  5. EQnoble

    EQnoble Well-Known Member

    I should note that this has to run after the page finishes loading if that makes a difference
     
  6. EQnoble

    EQnoble Well-Known Member

    hah i am such a nubcake...and just did some reading.

    Is this how I should call it if possible?
    Code:
    <script type="text/javascript">
    
    $(function()
    {
    myThing();
    });
    
    </script>
    update:

    I tested it and it loads fine so if it looks like the preferable way I believe I am ready to release my new addon I believe.
     
  7. James

    James Well-Known Member

    Looks fine in theory Ant. Question: Why are you using window.onload if it's loading last? Why not use $(document).ready instead?
    http://api.jquery.com/ready/
    Something like this (untested):
    Code:
    $(document).ready(function() {
        myThing() // remove the onload code from your function and just call the raw function
    });
    
     
    EQnoble likes this.
  8. EQnoble

    EQnoble Well-Known Member

    This also works...thanks James. I am going to release the mod using this...I probably should have mentioned before that I needed it to run after the doc is loaded. Awesome.
     
    James likes this.
  9. Kier

    Kier XenForo Developer Staff Member

    $(document).ready(function() { myThing(); });

    is identical to

    $(function() { myThing(); });
     
    EQnoble likes this.
  10. Kier

    Kier XenForo Developer Staff Member

    By the way, onload and DOMready are not the same thing. Very few things should ever use window.onload (or $(window).load(...); ) because it only fires after everything has finished loading - images, ads, the lot. DOMready is far more useful - and that is what is used by $(function() { ... });
     
    EQnoble likes this.
  11. EQnoble

    EQnoble Well-Known Member

    Hmm i am kind of stuck then I don't know what I should be using then...

    So let me see if I understand this correctly Kier...

    I am thinking DOMready will fire after the construction of the page is loaded while window.onload will fire only after the page hierarchy and the contents are fully loaded.

    So if my addon can run using DOMready I should use that by default?
     
  12. Kier

    Kier XenForo Developer Staff Member

    DOMready fires when the HTML has finished downloading. It does not wait for images, CSS, async-JS, Flash, ads etc.

    Unless you have a very good reason to use onLoad, use DOMready instead:
    Code:
    $(function()
    {
    alert('I will fire on DOM ready');
    });
     
    EQnoble likes this.
  13. James

    James Well-Known Member

    It doesn't actually matter which declaration you use does it?
     
  14. Kier

    Kier XenForo Developer Staff Member

    Nope. They are identical.
     

Share This Page