#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    How i you write external .js scripts?


    hello all,

    I am just starting to learn the java lingo. done abit on codeacademy and have just started on the w3schools site today.

    Anyways i have come across the external scripting part and basically my question is what JavaScript will i put in the "myScript.js" file? i tried to write my own .js file, i put a function in it to change the "A Paragraph." text on the button click

    Code:
    <html>
    <body>
    
    <h1>My Web Page</h1>
    
    <p id="demo">A Paragraph.</p>
    
    <button type="button" onclick="myFunction()">Try it</button>
    
    <p><strong>Note:</strong> myFunction is stored in an external file called "myScript.js".</p>
    
    <script type="text/javascript" src="myScript.js"></script>
    
    </body>
    </html>
    Sorry if that made no sense, i am very very green when it comes to all of this (as you can probably tell :P)

    Any help is much appreciated.
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1712
    I'll never forgive Netscape and Sun for the marketing driven rename of Mocha and LiveScript to JavaScript. Regardless of how they sound, JavaScript and Java have nothing really in common other than they are both programming languages. They are aimed at very different areas. Take a look at this post for a bit more information.

    Nevertheless, many of us bridge both worlds. You would write in your JavaScript file any code that you might normally put into an HTML file. For example (derived from here)

    html Code:
     
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function myFunction() {
        alert("Hello! I am an alert box!");
    }
    </script>
    </head>
    <body>
     
    <input type="button" onclick="myFunction()" value="Show alert box" />
     
    </body>
    </html>


    Or...

    The file hello.html
    html Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript" src="hello.js"></script>
    </head>
    <body>
     
    <input type="button" onclick="myFunction()" value="Show alert box" />
     
    </body>
    </html>


    and the file hello.js

    javascript Code:
    function myFunction() {
        alert("Hello! I am an alert box!");
    }


    You're simply putting the JavaScript into a separate file.

IMN logo majestic logo threadwatch logo seochat tools logo