Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
896 views
in Q2A Core by

Can someone explain what this does:

		var qa_oldonload=window.onload;
		window.onload=function() {
			if (typeof qa_oldonload=='function')
				qa_oldonload();

You find it on every question page, seems to be related to CKEditor or editors?

See also qa-page.php

For me it seems that the onload is called several times...

Thanks,
Kai

Q2A version: 1.6.3
by
I removed the lines
// "\tif (typeof qa_oldonload=='function') {", // eetv: added {}
// "\t\tqa_oldonload(); }"
from qa-page.php

It seems - for now - that there are no sideeffects...

1 Answer

0 votes
by
selected by
 
Best answer

window.onload is an event called when the page has finished loading. But if I attach one function to it and then attach another function later, it overrides the first one. Example:

window.onload = function() { alert('First'); }
window.onload = function() { alert('Second'); }

Only the second alert will run.

What the code in your question does is save the first function to a variable then assigns window.onload. Now if there was anything already set on window.onload it won't run, but because we saved the variable we can run the first one.

In short, it allows us to run JavaScript after the page loads without breaking someone else's code.

by
Yes I agree and this will probably happen in the future. Gideon didn't want to add a dependency on jQuery but I think jQuery is now ubiquitous enough that it's not an issue.
by
On all my x00 websites I'm using jquery. Also all my q2a plugins depend on jquery. The library, as you know, is so powerful.
by
"Plus more urgent: ALL existing javascript code should be transferred to Jquery code! What pain it is to still use pure Javascript with q2a!" ==> AGREE (mostly, as pure JS is faster :P). Even better, there should be an array or string inside the qa_content array that goes directly into a jquery document.ready function when processed by the base theme.

That way you would add new jQuery code without the need of wrapping it inside this function manually, as the core would be doing so. This should be useful in many scenarios
by
Yes pure JS is faster, but if it's 5 times the amount of code I think it balances out :)
...