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

    Join Date
    Nov 2009
    Posts
    31
    Rep Power
    6

    JavaScript equivalent of HTML if


    In HTML you can have:

    <!--[if IE 6]><![endif]-->

    There is an equivalent in JavaScript?
    Can I have a JS file where I have an if statement that tells the browser to either ignore or recognize JS?
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    For IE? Have IE run/not run the code while other browsers do the opposite?
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    You can wrap a <script> tag in an HTML if statement.

    There is no direct equivalent within JavaScript, although there are various methods for guessing.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    31
    Rep Power
    6
    The if statement was just an example.

    I am wanting to do something like

    if(document.domain == "example.com")
    {

    //Insert JavaScript library here for use by other JS

    }
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    If you want to dynamically trigger the loading of a remote JavaScript file from JavaScript code you can do so by appending another <script> tag to the document. Here's one method of doing that:
    Code:
    var script = document.createElement('script');
    script.type= 'text/javascript';
    script.src= 'url';
    document.getElementsByTagName('head')[0].appendChild(script);
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    31
    Rep Power
    6
    Problem with that solution is all my javascript has to be stored in one javascript file.
  12. #7
  13. hack of all trades
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2009
    Location
    Madrid
    Posts
    900
    Rep Power
    517
    you could use the html if to add a class to your body
    Code:
    <!--[if lt IE 7]><!--> <body class="ie"> <!--<![endif]-->
    <!--[if gt IE 6]><!--> <body> <!--<![endif]-->
    then test for it in js
    eg (jquery)
    Code:
    var is_ie = $(body).hasClass('ie');
    if(is_ie) {...
    a: true is 1 and false is 0 right?
    b: 1
  14. #8
  15. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    I really have no idea what you're trying to do then. If you're asking whether JavaScript supports if statements the answer is yes.

    Comments on this post

    • haydenchambers agrees : i THINK theyre just looking for easy browser detection in JS
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  16. #9
  17. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,009
    Rep Power
    2791
    Originally Posted by E-Oreo
    I really have no idea what you're trying to do then. If you're asking whether JavaScript supports if statements the answer is yes.
    As E-Oreo says, you need to ask a better question. If you are unsure what it is you require to achieve your goal, state what it is you need to achieve.
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.

IMN logo majestic logo threadwatch logo seochat tools logo