...

View Full Version : Javascript not working when external



Hayes
05-16-2012, 04:39 PM
Hi all me again :thumbsup:

I have this bit of code:

<script type="text/javascript">
var testvar = "<? print $cards; ?>";
var name = "<? print $name; ?>";
//Create JavaScript array
var testarray = new Array();
//Fill JavaScript array from the converted string
testarray = testvar.split(":#:");
var imagesleft = 10;
var numclicks = 0;
var score = 0;
function clickCard (what,where)
{
var count=0;
for(var x = 0; x < testarray.length; x++)
{
if(what == testarray[x])
{
count++;
}
}
if(count==1)
{
alert("unique");
document.images[where].src = "./images/blank.png";
imagesleft--;
score = score+20;
clicks.innerHTML = "Unique Images to find = " + imagesleft;

if(imagesleft==0)
{
score = score - numclicks;
var r=confirm("Do you want to enter your score in the highscore table?");
if (r==true)
{
document.getElementById('score').value = score;
document.forms["highscore"].submit();
}
else
{
var t=confirm("Do you want to play again?");
if (t==true)
{
document.location.reload();
}
else
{
window.close();
}
}

}
}
else
{
alert("Not unique");
}
numclicks++;
pairs.innerHTML = "Number of Clicks = " + numclicks;
}
</script>

which works a treat but when it is put into a external js file all I get is the alert Not unique and nothing else! any ideas why?

EpicWebDesign
05-16-2012, 05:01 PM
Can you post the code for the page you are using where you are trying to call the script externally? That may help to narrow down the problem.

DanInMa
05-16-2012, 05:41 PM
using the php variables in the external version won't work becuase they are processed separately from the actual php file you are calling it from. Your current versions works becuase the php variabels are added when the page is requested.

you could try using global js variables instead and jsut define the variables on the page itself like you are now(via php) and include the external js that will use those variables.

Hayes
05-16-2012, 08:23 PM
Thanks that helped but I have noticed some major issues with the different browsers. Everything works fine in Chrome but the timer doesnt work in IE and in FF the number of clicks and cards left to find dont work. Any help?

Hayes
05-16-2012, 08:24 PM
URL is dannystones.com/game

felgall
05-16-2012, 08:52 PM
If you want to use PHP in an external JavaScript then give the file a .php extension instead of .js



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum