1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Beginner question: external scripts

Discussion in 'JavaScript' started by thrasher585, May 15, 2015.

  1. #1
    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.
     
    thrasher585, May 15, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    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
     
    PoPSiCLe, May 15, 2015 IP
  3. thrasher585

    thrasher585 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    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)?
     
    thrasher585, May 15, 2015 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    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.
     
    PoPSiCLe, May 16, 2015 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    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..).
     
    ThePHPMaster, May 16, 2015 IP
  6. Md Hannan

    Md Hannan Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #6
    try another code and search in other question in another blog ..
     
    Md Hannan, May 16, 2015 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #7
    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?
     
    deathshadow, May 17, 2015 IP
  8. thrasher585

    thrasher585 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    I changed from XAMPP to MAMP and it works properly. I think it had something to do with file permissions! :confused:
     
    thrasher585, May 22, 2015 IP
  9. Jake The Competition Man

    Jake The Competition Man Active Member

    Messages:
    451
    Likes Received:
    38
    Best Answers:
    1
    Trophy Points:
    63
    #9
    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.