Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  177] [ 7]  / answers: 1 / hits: 5949  / 9 Years ago, thu, april 2, 2015, 12:00:00

I implemented a sequential fade triggered by window.onload



I want to know if I can stop it from executing midway or wherever it currently is and show everything at once using ...style.display = inline-block; for everything that was going to be faded in.


More From » click

 Answers
5

If you have used,



function init()
{
}
window.onload = init;


Stop it!

That line of code will completely wipe out any other functions that were attached and ready to handle the onload event. How arrogant of your script to do so! ;)



Instead, your script should learn to play nicely. Unfortunately, JavaScript doesn't support the delegate syntax that C# has.

Yes, there is a way to achieve that :



First Way:



function highlightXFNLinks()
{
// Does stuff...
}

//
// Adds event to window.onload without overwriting currently
// assigned onload functions.
function addLoadEvent(func)
{
var oldonload = window.onload;
if (typeof window.onload != 'function')
{
window.onload = func;
}
else
{
window.onload = function()
{
oldonload();
func();
}
}
}

addLoadEvent(highlightXFNLinks);

window.onload += init;


Second Way:



Better than first one...



function AddOnload(myfunc)
{
if(window.addEventListener)
window.addEventListener('load', myfunc, false);
else if(window.attachEvent)
window.attachEvent('onload', myfunc);
}

[#38190] Wednesday, April 1, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alonso

Total Points: 747
Total Questions: 108
Total Answers: 105

Location: Mauritania
Member since Sun, Sep 25, 2022
2 Years ago
;