-2

If.... Else If.... is not giving any Output in javascript

Acctually I am matching navigation.userAgent string of javascript with some predefined strings using If.... Else If..... statement but the script is to long about 1.76 MB The script contains some jquery as well as javascript codes At the time of execution I didn't get any output if condition is true as defined in If.... Else If.... statement code block. Please suggest some simple and working solution any help must be appreciated.

Edit 01

The script is attached to my Index.html with <script> tag's src attribute.

The actual code block is very large it is just a small code snippet of that script but the code beyond is completely similar like this one.

$(function(){
 var winURL = $("body").attr("winredirection");
 var androidURL = $("body").attr("androidredirection");
 var mobURL = $("body").attr("mobredirection");
 var bbURL = $("body").attr("bbredirection");

 if (navigator.userAgent=="Mozilla/5.0 (compatible; U; ABrowse 0.6;Syllable) AppleWebKit/420+ (KHTML, like Gecko)") {
   window.location.assign($("body").attr("winredirection"));
 }
 else if (navigator.userAgent=="Mozilla/5.0 (compatible; ABrowse 0.4; Syllable)") {
   window.location.assign($("body").attr("winredirection"));
 }
 else if (navigator.userAgent=="Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; Acoo Browser 1.98.744; .NET CLR 3.5.30729)") {
   window.location.assign($("body").attr("winredirection"));
 }
 else if (navigator.userAgent=="Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; Acoo Browser 1.98.744; .NET CLR   3.5.30729)"){window.location.assign($("body").attr("winredirection"));}else if (navigator.userAgent=="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0;   Acoo Browser; GTB5; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;   SV1) ; InfoPath.1; .NET CLR 3.5.30729; .NET CLR 3.0.30618)"){window.location.assign($("body").attr("winredirection"));}else if (navigator.userAgent=="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; Acoo Browser; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; Avant Browser)"){window.location.assign($("body").attr("winredirection"));}

});

Edit 02

My Index.html page

<!DOCTYPE html> 
<html> 
<head> 
<title> 
  Platform Identification using perfect.api.js
</title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
<script src="perfect.api.js"></script> 
</head> 
<body winredirection="http://google.com"  androidredirection="www.google.com/mobile/android" mobredirection="www.google.com/mobile" bbredirection="www.google.com/mobile/bb"> 

</body> 
</html>

Thanks,

API Developer

steveukx
  • 4,370
  • 19
  • 27

2 Answers2

1

If your intention is to detect a mobile device and redirect as a result, you should check for specific tokens in the user-agent string instead of matching exactly, as exact matches are unlikely to match given that they will vary depending on the OS version, browser version and in some cases the software installed on the device.

To check for mobile devices, try the answer from Detecting a mobile browser

Community
  • 1
  • 1
steveukx
  • 4,370
  • 19
  • 27
0

Firstly I would definitely recommend to not manually parse user agent strings as the code can easily become a mess to maintain especially the way you are doing it with a whole bunch of if/else if statements. I would use a library like this: https://github.com/faisalman/ua-parser-js. Using it's getOs() function to determine what operating system is being used or it's other functions to determine what device is being used rather than matching the specific user agents.

The code that you currently have looks fine, I can't test it as I don't have any of the browsers that you have in the user agent strings provided in your example but I would recommend doing something like so using the library linked above:

var parser = new UAParser();
var os = parser.getOS();

if(os.name == "windows") {
 $("body").attr("winredirection");
}
//other operating systems and/or devices go below.
Gabriel Sadaka
  • 1,748
  • 1
  • 15
  • 19
  • I don't want to use any third party library – API developer Jun 14 '15 at 06:37
  • well you can write the code manually yourself, there are plenty of resources online about using regex to detect which device is being used. Why don't you want to use a third party library? – Gabriel Sadaka Jun 14 '15 at 06:38
  • I felt bad while using some other one's creation into my project – API developer Jun 14 '15 at 06:40
  • you are using jQuery which is created by someone else, so is your operating system, browser, Javascript and the list goes on. Open source code is out there to be used, feel free to contribute to it if that makes you feel better but not using it out of guilt will only hurt yourself. In development you will always be building on top of the work of others that is the nature of engineering, science and human innovation. – Gabriel Sadaka Jun 14 '15 at 06:43
  • Thank you so much for your solution and such a valuable eyeopener, I am agree with you but in this case I wanna to make my own API and by including any other library I think it will remove my credit from it? – API developer Jun 14 '15 at 06:49
  • If the API you are developing is exactly the same as the library mentioned I would recommend not writing it and instead contributing code to the library linked as it is open source on github allowing external contributions. If you are going to provide more features than it, there is no harm in using it as your base. You can even fork it on github (please google forking) and then add the features to it then contribute that back to the original library. – Gabriel Sadaka Jun 14 '15 at 06:53
  • I am but I prefer not to release those details publicly, I don't mind chatting in this comment thread for a while though – Gabriel Sadaka Jun 14 '15 at 07:01
  • 1
    Feel free to ask other questions on StackOverflow if you need help with other problems so that the whole community can participate – Gabriel Sadaka Jun 14 '15 at 07:03