• 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.

EQnoble

Well-known member
#1
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
 

EQnoble

Well-known member
#2
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>
 

Kier

XenForo Developer
Staff member
#3
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

Well-known member
#4
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 :)
 

EQnoble

Well-known member
#6
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.
 

James

Well-known member
#7
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.
Looks fine in theory Ant. Question: Why are you using window.onload if it's loading last? Why not use $(document).ready instead?
.ready( handler )

handler A function to execute after the DOM is ready.
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

Well-known member
#8
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.
 

Kier

XenForo Developer
Staff member
#10
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

Well-known member
#11
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?
 

Kier

XenForo Developer
Staff member
#12
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?
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');
});