Cross browser porblem in adding dynamically loaded javascrip
Posted: Fri Jun 19, 2009 3:05 am
I am trying to develop a card game using php and javascript. THe problem is that it is working perfectly fine in FF3 and IE7+, but fails to work in Opera and Chrome. And the worst thing is that da javascript debugger in opera gives no error and still the script stops working.
When the page load the "push_game" is function is called
The two files mentioned above namely "push_game.php" and "automove.php" returns javascript according to the data sent to it. The data sent by these files is like:
When the page load the "push_game" is function is called
Code: Select all
function push_game() {
var unixts = document.forms['checkmov']['lastmove'].value;
var Xhand = document.forms['checkmov']['hand'].value;
var Xmove = document.forms['checkmov']['tomove'].value;
var force = 0;
var url = document.location.href;
var xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);
thisurl = base_url + 'includes/push_game.php?ts='+unixts+'&h='+Xhand+'&m='+Xmove+'&f='+force;
checkloadfile(thisurl, "js");
thisurl = base_url + 'includes/auto_move.php';
checkloadfile(thisurl, "js");
setTimeout("push_game()", 1000);
}
Code: Select all
document.getElementById('nameofelement').innerHTML = '<table width="1" height="5"><tr><td><img src="images/spacer.gif" width="1" height="5"></td></tr></table>';
Code: Select all
function checkloadfile(filename, filetype){
if (filesadded.indexOf("["+filename+"]")==-1){
loadfile(filename, filetype);
filesadded+="["+filename+"]";
}
else{
replacefile(filename, filename, filetype);
}
}
function loadfile(filename, filetype){
if (filetype=="js"){
var fileref=document.createElement('script') ;
fileref.setAttribute("type","text/javascript") ;
fileref.setAttribute("src", filename) ;
}
else if (filetype=="css"){
var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet") ;
fileref.setAttribute("type", "text/css") ;
fileref.setAttribute("href", filename);
}
if (typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref);
}
function createfile(filename, filetype){
if (filetype=="js"){
var fileref=document.createElement('script') ;
fileref.setAttribute("type","text/javascript") ;
fileref.setAttribute("src", filename) ;
}
return fileref ;
}
function replacefile(oldfilename, newfilename, filetype){
var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" ;
var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" ;
var allsuspects=document.getElementsByTagName(targetelement);
for (var i=allsuspects.length; i>=0; i--){
if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
var newelement=createfile(newfilename, filetype);
//DEBUG CODE
$('#loading').text('Trying to reload: '+oldfilename);
//END DEBUG CODE
allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i]);
}
}
}