View Full Version : Browser detect & print script
FrickenTrevor
10-31-2009, 12:07 AM
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! :)
jscheuer1
10-31-2009, 03:59 AM
Give this a shot and adapt as required:
<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.
FrickenTrevor
10-31-2009, 08:44 PM
Thanks, but is there any way your example could be in a "document write" sort of thing instead of an alert box?
Example:
document.write('Internet Explorer 7');
jscheuer1
10-31-2009, 09:01 PM
document.write() and alert() in this case are basically interchangeable, ex:
<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:
<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.
FrickenTrevor
11-02-2009, 05:13 AM
Thanks, it works great.
I also noticed some comment tags in the script:
<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"?
jscheuer1
11-02-2009, 05:53 AM
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:
<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.
FrickenTrevor
11-02-2009, 11:06 PM
Thanks so much, this helped alot
:D
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.