When an ajax response is processed in "handleResponse" - code snippet below
..can someone explain why the inline <script> tags are extracted (and basically removed) from the html fragment and the html fragments added to a temp dom element before being inserted into the html page, and then the extracted inline scripts are then evaluated?Code:handleResponse: function(response, ioArgs) { ... ... //Extract and store all <script> elements from the response var scriptPattern = '(?:<script(.|[\n|\r])*?>)((\n|\r|.)*?)(?:<\/script>)'; var extractedScriptNodes = []; var matchAll = new RegExp(scriptPattern, 'img'); var matchOne = new RegExp(scriptPattern, 'im'); var scriptNodes = response.match(matchAll); if (scriptNodes != null) { for (var i=0; i<scriptNodes.length; i++) { var script = (scriptNodes[i].match(matchOne) || ['','',''])[2]; script = script.replace(/<!--/mg,'').replace(/\/\/-->/mg,'').replace(/<!\[CDATA\[(\/\/>)*/mg,'').replace(/(<!)*\]\]>/mg,''); extractedScriptNodes.push(script); } } // Remove scripts but don't remove scripts entirely (see SWF-1358) response = response.replace(matchAll, '<script> // Original script removed to avoid re-execution </script>'); if (modalView) { //For a modal view, just dump the response into a modal dialog Spring.remoting._renderResponseToModalDialog(response); } else { //Extract the new DOM nodes from the response var tempSpan = dojo.doc.createElement("span"); tempSpan.id="ajaxResponse"; tempSpan.style.display= "none"; document.body.appendChild(tempSpan); tempSpan.innerHTML=response; var tempContainer = new dojo.NodeList(tempSpan); var newNodes = tempContainer.query(">").orphan(); tempContainer.orphan(); //Insert the new DOM nodes and update the Form's action URL newNodes.forEach(function(item){ if (item.id != null && item.id != "") { var target = dijit.byId(item.id) ? dijit.byId(item.id).domNode : dojo.byId(item.id); if (!target) { console.error("An existing DOM elment with id '" + item.id + "' could not be found for replacement."); } else { target.parentNode.replaceChild(item, target); } } }); } //Evaluate any script code dojo.forEach(extractedScriptNodes, function(script){ dojo.eval(script); }); return response;
I'm guessing some browsers do not execute inline scripts when added dynamically via javascript?
But why is temp dom created? Why not add straight in the html page in the relevant place?


Reply With Quote