M
mowsen
Hello Group,
i'm using a little "ajax" loader script to dynamically load files into
different "div" tags on my main site. the code for this part looks
like:
function loader() {
var args = loader.arguments;
switch (args[0]) { case
"load_page":
if (document.getElementById) {
var x = (window.ActiveXObject) ? new
ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); //create
xmlhttp object
}
if (x) {
x.onreadystatechange = function() {
if (x.readyState == 4 && x.status == 200) {
el = document.getElementById(args[2]);
var viewData = x.responseText;
splitcode(el, viewData, args[2]);
}
}
x.open(args[3], args[1], true);
x.send(args[4]);
}
break;
}
}
i.e.: i call it through: loader('load_page', 'test/test.html', 'main',
'GET', 'null');
now, i want to get the javascript in test.html executed. for this
purpose i wrote splitcode(), which searches for <script> tags and
executes them.. this looks like:
function splitcode(el, viewData, id) {
var regexp1 = /<script(.|\n)*?>(.|\n|\r\n)*?<\/script>/ig;
var regexp2 = /<script(.|\n)*?>((.|\n|\r\n)*)?<\/script>/im;
var regexp3 = /<script src(.|\n)*?>(.|\n|\r\n)*?<\/script>/ig;
var regexp4 = /src.*\s\b/ig;
/* draw html first */
htmlpart = viewData.replace(regexp1, "");
el.innerHTML = htmlpart;
var result = viewData.match(regexp3);
if (result) {
for (var i = 0; i < result.length; i++) {
var srcScript = result.match(regexp4);
srcScript += "";
srcScript = srcScript.substr(5, srcScript.length-7);
var scriptContainer = document.createElement('SCRIPT');
var scriptContainerSrc = document.createAttribute('src');
var scriptContainerType = document.createAttribute('type');
scriptContainerSrc.value = srcScript;
scriptContainerType.value = "text/javascript";
scriptContainer.setAttributeNode(scriptContainerSrc);
scriptContainer.setAttributeNode(scriptContainerType);
document.getElementsByTagName("head")
[0].appendChild(scriptContainer);
}
}
var result = viewData.match(regexp1);
if (result) {
for (var i = 0; i < result.length; i++) {
var realScript = result.match(regexp2);
executeScript(realScript[2]);
}
}
}
function executeScript(scriptFrag) {
var scriptContainer = document.createElement('SCRIPT');
document.getElementsByTagName("head")
[0].appendChild(scriptContainer);
scriptContainer.text = scriptFrag;
}
This all works quite well within firefox. But for IE and Safari it
won't do at all! IE at least, lets me execute a function noted in
test.html once, but then it seems as if it has lost the javascript
code or can't find it again...
Any ideas to overcome this are greatly appreciated!
Moka Toka
i'm using a little "ajax" loader script to dynamically load files into
different "div" tags on my main site. the code for this part looks
like:
function loader() {
var args = loader.arguments;
switch (args[0]) { case
"load_page":
if (document.getElementById) {
var x = (window.ActiveXObject) ? new
ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); //create
xmlhttp object
}
if (x) {
x.onreadystatechange = function() {
if (x.readyState == 4 && x.status == 200) {
el = document.getElementById(args[2]);
var viewData = x.responseText;
splitcode(el, viewData, args[2]);
}
}
x.open(args[3], args[1], true);
x.send(args[4]);
}
break;
}
}
i.e.: i call it through: loader('load_page', 'test/test.html', 'main',
'GET', 'null');
now, i want to get the javascript in test.html executed. for this
purpose i wrote splitcode(), which searches for <script> tags and
executes them.. this looks like:
function splitcode(el, viewData, id) {
var regexp1 = /<script(.|\n)*?>(.|\n|\r\n)*?<\/script>/ig;
var regexp2 = /<script(.|\n)*?>((.|\n|\r\n)*)?<\/script>/im;
var regexp3 = /<script src(.|\n)*?>(.|\n|\r\n)*?<\/script>/ig;
var regexp4 = /src.*\s\b/ig;
/* draw html first */
htmlpart = viewData.replace(regexp1, "");
el.innerHTML = htmlpart;
var result = viewData.match(regexp3);
if (result) {
for (var i = 0; i < result.length; i++) {
var srcScript = result.match(regexp4);
srcScript += "";
srcScript = srcScript.substr(5, srcScript.length-7);
var scriptContainer = document.createElement('SCRIPT');
var scriptContainerSrc = document.createAttribute('src');
var scriptContainerType = document.createAttribute('type');
scriptContainerSrc.value = srcScript;
scriptContainerType.value = "text/javascript";
scriptContainer.setAttributeNode(scriptContainerSrc);
scriptContainer.setAttributeNode(scriptContainerType);
document.getElementsByTagName("head")
[0].appendChild(scriptContainer);
}
}
var result = viewData.match(regexp1);
if (result) {
for (var i = 0; i < result.length; i++) {
var realScript = result.match(regexp2);
executeScript(realScript[2]);
}
}
}
function executeScript(scriptFrag) {
var scriptContainer = document.createElement('SCRIPT');
document.getElementsByTagName("head")
[0].appendChild(scriptContainer);
scriptContainer.text = scriptFrag;
}
This all works quite well within firefox. But for IE and Safari it
won't do at all! IE at least, lets me execute a function noted in
test.html once, but then it seems as if it has lost the javascript
code or can't find it again...
Any ideas to overcome this are greatly appreciated!
Moka Toka