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

Can someone explain what this does:

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

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


Q2A version: 1.6.3
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
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.

Oh, and the reason removing those lines still works in your case is because there didn't happen to be any other code that was using window.onload. That's probably the case for most people, but it's a preventative measure.
Thanks for the info Scott. This reminds me of the idea of putting all the javascript code of all active plugins into one single file and only deliver this one frontend. (Same for CSS.) If we have implemented this milestone, the multiple onload will not be necessary anymore.
Plus more urgent: ALL existing javascript code should be transferred to Jquery code! What pain it is to still use pure Javascript with q2a!
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.
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.
"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
Yes pure JS is faster, but if it's 5 times the amount of code I think it balances out :)