Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    42
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Javascript not working when external

    Hi all me again

    I have this bit of code:
    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?

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Posts
    165
    Thanks
    1
    Thanked 39 Times in 39 Posts
    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.

  • #3
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    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.

  • Users who have thanked DanInMa for this post:

    Hayes (05-16-2012)

  • #4
    New Coder
    Join Date
    Nov 2007
    Posts
    42
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?

  • #5
    New Coder
    Join Date
    Nov 2007
    Posts
    42
    Thanks
    3
    Thanked 0 Times in 0 Posts
    URL is dannystones.com/game

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    If you want to use PHP in an external JavaScript then give the file a .php extension instead of .js
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •