SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Non-Member
    Join Date
    May 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wink Learn Javascript

    Let us start to share our javascript knowledge..

    A javascript should be
    <script language='javascript'>
    </script>

    Example Javascript:
    <script language='javascript'>
    alert('hi');
    </script>
    Last edited by DaveMaxwell; May 10, 2010 at 07:43. Reason: removed erroneous link....

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by srsabariselvan View Post
    Let us start to share our javascript knowledge..

    A javascript should be
    <script language='javascript'>
    </script>

    Example Javascript:
    <script language='javascript'>
    alert('hi');
    </script>
    Sorry but no.

    • The language attribute is long-since deprecated. It's original intention was to prevent older web browsers from running scripts that they wouldn't understand, which didn't work as well as was intended.
    • The type attribute is the accepted way to specify the type of script, although as javascript is the defacto default, even that can be left out
    • Double quotes are accepted standard for quoting html attributes
    • Inline scripting is to be avoided as much as possible, so the script should be loaded from an external file instead.


    That results in:

    Code html4strict:
    <script type="text/javascript" src="js/script.js"></script>

    which could even be reduced down to:


    Code html4strict:
    <script src="js/script.js"></script>

    The script would remain as:

    js/script.js
    Code javascript:
    alert('hi');
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,871
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you really want to make sure that the code only runs as JavaScript then you'd use:

    Code HTML4Strict:
    <script type="application/javascript" src="js/script.js"></script>

    text/javascript is deprecated but is commonly used because to use the correct application/jaascript means that IE will not attempt to run the code as JScript and since IE doesn't actually support JavaScript (only JScript and vbScript) it is unable to run the script if you use the "correct" JavaScript MIME type.
    The only time you'd use the language attribute on a script tag is for server side languages. For example the long form of the PHP script tag is:

    Code PHP:
    <script language="php">
    // PHP code goes here
    </script>

    The shorter <?php ?> version is more commonly used though.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Tags for this Thread

Bookmarks

Posting Permissions

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