Wrox Programmer Forums
|
BOOK: Beginning JavaScript 5th Edition
This is the forum to discuss the Wrox book Beginning JavaScript, 5th Edition by Jeremy McPeak; 978-1-118-90333-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript 5th Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 17th, 2017, 10:25 AM
Registered User
 
Join Date: May 2016
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Browser Sniffing problem

Hi,
The output is giving me 10 as value, whereas it should be 10.0
Here is the code:

HTML Code:
<script>
var browserString = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729)";
var browserName = "MSIE";
var findingIndex = browserString.indexOf(browserName) + browserName.length + 1;
var browserVersion = parseFloat(browserString.substring(findingIndex, findingIndex+3));
document.write(browserVersion);
</script>
 
Old February 21st, 2017, 08:02 PM
Registered User
 
Join Date: Feb 2017
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

parseFloat is working fine however if the value is after the decimal is 0 the it will not be converted. If it was any number more than 0 it would be converted to a decimal.

You can test this by changing the verion to .1 o .2 and you'll see the scring is converting to a number with a floating point fine.

If you absolutely need to see the 0 then you will have to add a toFixed() method.

Also it seems to me your substring is cont capturing the whole version. Change the sunbstring to substring(findingIndex, findingIndex+4).

So the total code will look like this:

HTML Code:
var browserString = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729)";
var browserName = "MSIE";
var findingIndex = browserString.indexOf(browserName) + browserName.length + 1;
var browserVersion = parseFloat(browserString.substring(findingIndex, findingIndex+4)).toFixed(1);
document.write(browserVersion);





Similar Threads
Thread Thread Starter Forum Replies Last Post
PROBLEM WITH XML DISPLAY IN BROWSER jceaser1254 XML 7 April 21st, 2007 03:45 AM
Problem in using Browser in Winforms jay69 C# 0 December 5th, 2006 05:49 AM
Problem with xml and layers in different Browser. diptanjan XML 0 March 9th, 2006 09:40 AM
Inherits Problem In Browser (Urgent) [email protected] ASP.NET 1.0 and 1.1 Basics 1 September 27th, 2005 09:24 AM
Multiple Browser Problem someshtrivedi General .NET 4 February 28th, 2005 12:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.