Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 17th, 2017, 09:25 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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>
Reply With Quote
  #2 (permalink)  
Old February 21st, 2017, 07:02 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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);
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 04:49 AM
Problem with xml and layers in different Browser. diptanjan XML 0 March 9th, 2006 08:40 AM
Inherits Problem In Browser (Urgent) helga@trisyn.co.za 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 11:09 AM



All times are GMT -4. The time now is 01:03 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.