How to run a javascript

Discussion in 'Web Design and Development' started by dinggus, Feb 6, 2013.

  1. macrumors 65816

    Joined:
    Jan 17, 2012
    #1
    How can I run a javascript just from my computer? I'd like to test a script out.
     
  2. macrumors 65816

    imaketouchtheme

    Joined:
    Dec 5, 2007
    Location:
    WV
    #2
    Google Chrome? View > Developer > Javascript Console
     
  3. dinggus, Feb 6, 2013
    Last edited: Feb 6, 2013

    thread starter macrumors 65816

    Joined:
    Jan 17, 2012
    #3
    Thanks, do I just enter the script info or what? Can I PM you the script code?
     
  4. macrumors regular

    Joined:
    Mar 27, 2009
    Location:
    Skipton, UK
    #4
    Simply create a web page and include the code. Save the file locally and double click it to open in your browser. If your script relies on certain elements being on the page then you'll have to add them.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <!-- put CSS links and meta tags here -->
    </head>
    <body>
    <!-- Any elements you need for your script to work -->
    <script src = "path to your script"></script>
    </body>
    </html>
    
    Depending on how you've written your script you may have to add an event handler to the body:

    Code:
    <body onload="entryfunction()">
    Better is to use the IIFE (Immediately Invoked Function Expression) format at the foot of the page:

    Code:
    <script>
    (function (){
    // All your code here or call your entry function.
    }());
    </script>
    
     
  5. macrumors 65816

    Joined:
    Apr 17, 2012
    Location:
    Destin, FL
    #5
  6. macrumors member

    Joined:
    May 28, 2012
    Location:
    Blighty
    #6
    If your code depends on other libraries such as jQuery, you can try it out online at jsfiddle.net

    You can then share it with other people if you want to show them about your code (e.g. to get help with it).
     
  7. thread starter macrumors 65816

    Joined:
    Jan 17, 2012
    #7
    Thanks I shared it with Dunmail and his response was:
    So, now I'm stuck because I how no clue how who to get ahold of for the script, or create a web page with buttons to call the functions.
     

Share This Page