Skip to content

Commit

Permalink
worksheet theme waits for document ready before init
Browse files Browse the repository at this point in the history
The base start-exam.js used to use jQuery's load function to wait for the document to be loaded before doing the init stuff.

The worksheet theme didn't do anything, so there could be a timing problem if the code ran before the page had finished loading.

This adds a function Numbas.util.document_ready, which uses the native document.readyState property to wait until the page has finished loading, and changes both versions of start-exam.js to use it.
  • Loading branch information
christianp committed Jul 9, 2024
1 parent 524661e commit e35aadb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
4 changes: 2 additions & 2 deletions runtime/scripts/start-exam.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Copyright 2011-14 Newcastle University
// 'base' gives the third-party libraries on which Numbas depends
Numbas.queueScript('base',['jquery','localisation','seedrandom','knockout','sarissa'],function() {
});
Numbas.queueScript('start-exam',['base','exam','settings'],function() {
Numbas.queueScript('start-exam',['base','util', 'exam','settings'],function() {
for(var name in Numbas.custom_part_types) {
Numbas.partConstructors[name] = Numbas.parts.CustomPart;
};
Expand Down Expand Up @@ -43,7 +43,7 @@ Numbas.queueScript('start-exam',['base','exam','settings'],function() {
* @function
*/
var init = Numbas.init = function() {
$(document).ready(function() {
Numbas.util.document_ready(function() {
for(var x in Numbas.extensions) {
Numbas.activateExtension(x);
}
Expand Down
16 changes: 16 additions & 0 deletions runtime/scripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ Copyright 2011-14 Newcastle University
Numbas.queueScript('util',['base', 'math', 'parsel'],function() {
/** @namespace Numbas.util */
var util = Numbas.util = /** @lends Numbas.util */ {
/** Run the given function when the document is ready.
*
* @param {Function} fn
*/
document_ready: function(fn) {
if(document.readyState == 'complete') {
setTimeout(fn, 1);
} else {
document.addEventListener('readystatechange', function(e) {
if(document.readyState == 'complete') {
setTimeout(fn, 1);
}
});
}
},

/** Derive type B from A (class inheritance, really)
*
* B's prototype supercedes A's.
Expand Down
52 changes: 27 additions & 25 deletions themes/worksheet/files/scripts/start-exam.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
// 'base' gives the third-party libraries on which Numbas depends
Numbas.queueScript('base',['jquery','localisation','seedrandom','knockout','sarissa'],function() {
});


Numbas.queueScript('start-exam',['base','exam','settings'],function() {
for(var name in Numbas.custom_part_types) {
Numbas.partConstructors[name] = Numbas.parts.CustomPart;
};

Numbas.init = function() {
for(var x in Numbas.extensions) {
Numbas.activateExtension(x);
}
var job = Numbas.schedule.add;

job(Numbas.xml.loadXMLDocs); //load in all the XML and XSLT files

job(function() {
var store = Numbas.store = new Numbas.storage.scorm.SCORMStorage(); //The storage object manages communication between the LMS and the exam
Numbas.display.init();
});
};
});

// 'base' gives the third-party libraries on which Numbas depends
Numbas.queueScript('base',['jquery','localisation','seedrandom','knockout','sarissa'],function() {
});


Numbas.queueScript('start-exam',['base','exam','settings'],function() {
for(var name in Numbas.custom_part_types) {
Numbas.partConstructors[name] = Numbas.parts.CustomPart;
};

Numbas.init = function() {
Numbas.util.document_ready(function() {
for(var x in Numbas.extensions) {
Numbas.activateExtension(x);
}
var job = Numbas.schedule.add;

job(Numbas.xml.loadXMLDocs); //load in all the XML and XSLT files

job(function() {
var store = Numbas.store = new Numbas.storage.scorm.SCORMStorage(); //The storage object manages communication between the LMS and the exam
Numbas.display.init();
});
});
};
});

0 comments on commit e35aadb

Please sign in to comment.