From 7470d6de7ab54cc22cc3659a354c31647c7d8bf5 Mon Sep 17 00:00:00 2001 From: sam_marshall Date: Mon, 25 Jun 2007 14:38:02 +0000 Subject: [PATCH] MDL-10249 Created addonload JavaScript function that can be used to add onload handlers, instead of using which breaks things. --- lib/javascript-static.js | 14 ++++++++++++++ lib/javascript.php | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/javascript-static.js b/lib/javascript-static.js index 4b7e1450b7..a5f3194b0f 100644 --- a/lib/javascript-static.js +++ b/lib/javascript-static.js @@ -420,3 +420,17 @@ function insertAtCursor(myField, myValue) { myField.value += myValue; } } + + +/* + Call instead of setting window.onload directly or setting body onload=. + Adds your function to a chain of functions rather than overwriting anything + that exists. +*/ +function addonload(fn) { + var oldhandler=window.onload; + window.onload=function() { + if(oldhandler) oldhandler(); + fn(); + } +} diff --git a/lib/javascript.php b/lib/javascript.php index 9526520b51..16de59881b 100644 --- a/lib/javascript.php +++ b/lib/javascript.php @@ -78,11 +78,12 @@ function inserttext(text) { if(($pos = strpos($focus, '.')) !== false) { //old style focus using form name - no allowed inXHTML Strict $topelement = substr($focus, 0, $pos); - echo "function setfocus() { if(document.$topelement) document.$focus.focus(); }\n"; + echo "addonload(function() { if(document.$topelement) document.$focus.focus(); });\n"; } else { //focus element with given id - echo "function setfocus() { if(el = document.getElementById('$focus')) el.focus(); }\n"; + echo "addonload(function() { if(el = document.getElementById('$focus')) el.focus(); });\n"; } + $focus=false; // Prevent themes from adding it to body tag which breaks addonload(), MDL-10249 } ?> //]]> -- 2.39.5