Results 1 to 7 of 7

Thread: Browser detect & print script

  1. #1
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Post Browser detect & print script

    Is there some type of script that will display a broswer version on a site?
    Example: Internet Explorer 7

    I only need this for IE, nothing else. I would prefer if it only said the
    Italic words, not "You are using MISE......". I would also perfer if the code was as small as possible.

    Note W3Schools did not help. Nor did Java-Scripts.net, javascriptkit.com, or quirksmode.org.

    Thanks!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Give this a shot and adapt as required:

    Code:
    <script type="text/javascript">
    /*@cc_on @*/
    /*@if(@_jscript_version >= 5)
    alert('Internet Explorer ' + navigator.userAgent.replace(/^.*MSIE[^\d]*([\d\.]*).*$/, '$1'));
    @end @*/
    </script>
    Any questions, feel free to ask.
    Last edited by jscheuer1; 10-31-2009 at 05:11 AM. Reason: added something wrong, then got rid of it
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Post

    Thanks, but is there any way your example could be in a "document write" sort of thing instead of an alert box?

    Example:
    Code:
    document.write('Internet Explorer 7');

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    document.write() and alert() in this case are basically interchangeable, ex:

    Code:
    <script type="text/javascript">
    /*@cc_on @*/
    /*@if(@_jscript_version >= 5)
    document.write('Internet Explorer ' + navigator.userAgent.replace(/^.*MSIE[^\d]*([\d\.]*).*$/, '$1'));
    @end @*/
    </script>
    The only difference being that when using document.write(), you must place the script in the flow of the page where you want the written text to appear.

    Another refinement, if you only want sub version numbers (.0 in the case of IE 7) displayed for those IE browsers that actually have them (IE has had no sub version numbers since IE 6) use it like so:

    Code:
    <script type="text/javascript">
    /*@cc_on @*/
    /*@if(@_jscript_version >= 5)
    document.write('Internet Explorer ' + (navigator.userAgent.replace(/^.*MSIE[^\d]*([\d\.]*).*$/, '$1') - 0));
    @end @*/
    </script>
    This way it will strip the sub version (the decimal point and the 0 following it) unless there is a number there after the decimal point that is not 0.
    Last edited by jscheuer1; 10-31-2009 at 09:08 PM. Reason: add refinement
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    FrickenTrevor (11-02-2009)

  6. #5
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    Thanks, it works great.

    I also noticed some comment tags in the script:

    Code:
    <script type="text/javascript">
    /*@cc_on @*/
    /*@if(@_jscript_version >= 5)
    alert('Internet Explorer ' + navigator.userAgent.replace(/^.*MSIE[^\d]*([\d\.]*).*$/, '$1'));
    @end @*/
    </script>
    is this some sort of advanced "hide from older broswers"?

  7. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Not exactly. It is a form of comment that is documented (not a hack) for IE 5+ that actually excludes all other browsers from executing the code inside:

    Code:
    <script type="text/javascript">
    /*@cc_on @*/ // <-- Turns on the comment engine in IE 5+ and exits comment mode
    /*@if(@_jscript_version >= 5) // <-- Reenters comment mode and Specifies the jScript version
    alert('Internet Explorer ' + navigator.userAgent.replace(/^.*MSIE[^\d]*([\d\.]*).*$/, '$1')); // <-- IE 5+ specific code execution
    @end @*/ // <-- exits comment mode
    </script>
    All other browsers see the code inside this construct as an ordinary script comment.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. The Following User Says Thank You to jscheuer1 For This Useful Post:

    FrickenTrevor (11-02-2009)

  9. #7
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Talking

    Thanks so much, this helped alot

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
  •