I’ve created two simple testpages. One loads the other one using XMLHttpRequest. (just a page using slimbox to display a image)
This works perfect the first time. But when trying to run it again, by pressing the button again, it does not work anymore. When I reload the whole page, it works the first time again. Do I have to un-load or re-load the scripts or something?
I know It is double of the script-tags here, don’t know why but its the only way I got it to work in the first place. How come it only works the first time?
Please help.
testajax.php
<html>
<head>
<link rel="stylesheet" href="SlimBox/css/slimbox.css" type="text/css" media="screen" />
<script id="script1" type="text/JavaScript"></script>
<script id="script2" type="text/JavaScript"></script>
<script type="text/javascript" src="SlimBox/js/mootools.js"></script>
<script type="text/javascript" src="SlimBox/js/slimbox.js"></script>
<script type="text/javascript">
function loadXMLDoc(Page)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDivFull").innerHTML=xmlhttp.responseText;
document.getElementById('script1').src = 'SlimBox/js/mootools.js';
document.getElementById('script2').src = 'SlimBox/js/slimbox.js';
}
}
xmlhttp.open("GET",Page,true);
xmlhttp.send();
}
</script>
</head>
<body>
<input id="Header2" type="button" onClick="loadXMLDoc('testImage.php')" name='Sok' value='Load' >
<div id="myDivFull"></div>
</body>
</html>
testImage.php
<a href='img/facebook-icon.png' rel='lightbox' cap='test'><img alt='' border='0' src='img/facebook-icon.png' /></a>
EDIT:
The method provided by gilly3 worked perfectly with the seperate js file, however I can not get it to work if I add a google gadget script ‘link’ into the function. Is this solved in a different way maybe? Below is the code I can not get to work.
function addScript(src)
{
var script = document.body.appendChild(document.createElement("script"));
script.src = src;
}
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDivFull").innerHTML=xmlhttp.responseText;
addScript('//www.gmodules.com/ig/ifr?url=http://www.mortgage-info.com/gadgets
/gadgetsmortgagecalculator.xml&synd=open&w=250&h=200&
amp;title=Mortgage+Calculator&border=%23ffffff%7C3px%2C1px+solid+%23999999&
amp;output=js');
}
Thank you.
Surely there’s a better way to accomplish what you are trying to do. But, I don’t know what you are trying to do, and there is a simple answer to your question:
Don’t bother with creating the empty script tags. Just add as many script tags as you like dynamically:
Edit: The “better way” would be to create a function that does what you want, and call it whenever you need to. Eg, if your script contained:
You would just create a function:
Then call it each time you receive your AJAX data.