Hello, I am trying to run an external script in JavaScript. It is located in a folder called "js" in the same directory as the main script. Here is my code: Main page (index.php) <script src="js/numeric.js"></script> Code (markup): External script (numeric.js) window.alert(5 + 6); Code (markup): Nothing happens! However, if I put the code in script tags on the main page, it works: Main page (index.php) <script> window.alert(5 + 6); </script> Code (markup): What is the solution? Thanks.
It works just fine. How is the rest of your document structured? Did you include a doctype? Call the script at the right place? This example: <!DOCTYPE html> <html> <head> <title>Test external JS</title> <script src="js/numeric.js"></script> </head> <body> <h1>This is a heading</h1> </body> </html> Code (markup): works just fine
Strange, it still isn't working. index.php <!DOCTYPE html> <html> <head> <title>Test external JS</title> <script src="js/numeric.js"></script> </head> <body> <h1>This is a heading</h1> </body> </html> Code (markup): numeric.js window.alert(5 + 6); Code (markup): All I get is "This is a heading". Could the problem be with XAMPP (Mac version)?
That I have no clue about - I'm using WAMP on a Windows-box. However, are you sure the js-directory is where you think it is? Ie, is the js-directory actually under the root-folder where your script is? You should have a look at the site with Firebug or similar, or even "inspect element" in most browsers, to see if they actually even see the js-script you're linking to.
I find it helpful to install Firebug extension and use console.log() to debug JS issues. It also makes it easy for me to show me any errors that occur (file not found, permissions issues, etc..).
That would be my suspicion, is that the path isn't resolving in relation to where your html document is located. remove the "js/" part and put the javascript file in the same directory/path that the HTML is being served from. Does it work then?
I changed from XAMPP to MAMP and it works properly. I think it had something to do with file permissions!
the only solution to this is that the path is wrong or you can't access that directory. We are all testing and saying you that the script works that way. check your directory structure and find what's wrong.