luke-sully
luke-sully

Reputation: 323

How to interchange between JS scripts using conditional comments

So I have a popup that opens from my home page through a "?" link. I wan run one script (Help.htm) to run if the user is using chrome, FF, Opera etc and a second script (Help_IE.htm) to run if the user is using IE.

js

 <script type="text/javascript">
            function popup() {
                window.open("Help.htm", "Help", "scrollbar=yes,location=yes,width=500,height=420");
            }
        </script>

html

    <a href="javascript:popup()">?</a>

I want to be able to interchange the JS scripts depending on the browser using conditional comments or if anyone has a more efficient answer, i'm open to anything, Thanks.

Attempt

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
<!--[if IE]>
    <script type="text/javascript">
    function popup() {
        window.open("Help_IE.htm", "Help", "scrollbar=yes,location=yes,width=500,height=420");
    }
    </script>
<![endif]-->

<script type="text/javascript">
    function popup() {
        window.open("Help.htm", "Help", "scrollbar=yes,location=yes,width=500,height=420");
    }
</script>

Upvotes: 2

Views: 57

Answers (2)

maioman
maioman

Reputation: 18762

You can use conditional if in function if you knew how to query a variable with the browser you're using ( and to do this you could go with something like modernizer) ;

but in your situation you could also try wrapping your script in html conditionals (like you'd do with CSS - it should work);

for ex. IE:

 <!--[if IE]>
 <script type="text/javascript">
        function popup() {
            window.open("Help.htm", "Help", "scrollbar=yes,location=yes,width=500,height=420");
        }
       </script>
  <![endif]-->

for non IE:

 <!--[if !IE]>
 <script type="text/javascript">
        function popup() {
            window.open("Help.htm", "Help", "scrollbar=yes,location=yes,width=500,height=420");
        }
     </script>
 <![endif]-->

Upvotes: 1

Faiz Shukri
Faiz Shukri

Reputation: 95

You can use conditional comments if you want. Alternatively, check this answer https://stackoverflow.com/a/13480430/1410583

Upvotes: 0

Related Questions