-1

I am looking to change "var $siteURL" & data "SITE: value".

If desktop environment, change code to "DES001". Otherwise if its mobile environment, change code to "MOB001".

By default, desktop siteURL and site data value works fine.

JS:

var $siteCode = 'DES001';

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
  alert("Mobile Device");
  var $siteCode = 'MOB001';
}

$('#SubmitForm').click(function() { 
  var $linkURL = 'http://www.test.com/SITE=',
      $siteURL = $siteCode,
      $webServiceURL = $linkURL + $siteURL;

  var $data = { 
    ARRANGE: 'DDD',
    BOOKING: 'CASH',
    SITE: $siteCode,
  }

 var $tripFlowUrl = $webServiceURL + $.param($data);
 alert($tripFlowUrl);
});
halfer
  • 19,824
  • 17
  • 99
  • 186
TDG
  • 1,294
  • 4
  • 18
  • 50
  • Hi @David.. Mobile detection is working for me.. Please let me know how to change SiteURL & Site data value based on detection. Thats the only thing pending for me – TDG Nov 14 '16 at 15:56
  • The entire question seems to focus heavily on mobile detection. Best to remove the unrelated things and focus specifically on the problem at hand, which is setting and using a variable in and out of an `if` block. – David Nov 14 '16 at 15:59

2 Answers2

0

You're declaring your variable in the wrong scope:

if (/.../) {
    var $mobileSiteCode = '';
}

// can't use $mobileSiteCode here, it's out of scope

Declare the variable in the scope you need it, then simply set the value within the conditional statement:

var $mobileSiteCode = '';
if (/.../) {
    $mobileSiteCode = 'some value';
}

// now you can use $mobileSiteCode here
David
  • 208,112
  • 36
  • 198
  • 279
  • Hi David.. Thanks for your help. But, now can i change this $mobilesiteCode or $desktopsiteCode values dynamically on click function & data value? This is the solution i am looking for. Thanks. Very sorry to trouble you. – TDG Nov 14 '16 at 16:10
  • @TDG: You can change the values anywhere that you can access the values. The code in the question may be a bit incomplete for what you're asking now. But as long as the variables are in scope you can modify them. Do you have a minimal example of something that isn't working? – David Nov 14 '16 at 16:25
  • i change my above codes like you mentioned. Now, even desktop or mobile, it shows only "DES001". even in mobile environment, its not getting "MOB001" – TDG Nov 14 '16 at 16:31
  • @TDG: When you debug this, where specifically does it fail? Given the code and the description in your last comment, it sounds like the `if` statement isn't resolving to `true`, though you'd previously claimed that the mobile browser detection was working as expected. – David Nov 14 '16 at 16:32
  • Thanks for your hints. finally i fixed the issue. Please check my below code. – TDG Nov 14 '16 at 16:43
0
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
 console.log("Mobile Device");
 var $siteCode = 'MOB001';
}
else{
 console.log("Desktop Device");
 var $siteCode = 'DES001';
}

function submitForm(){
 $('#SubmitForm').click(function() { 
  var $linkURL = 'http://www.test.com/SITE=',
      $siteURL = $siteCode,
      $webServiceURL = $linkURL + $siteURL;

  var $data = { 
   ARRANGE: 'DDD',
   BOOKING: 'CASH',
   SITE: $siteCode,
  }

  var $tripFlowUrl = $webServiceURL + $.param($data);
  alert($tripFlowUrl);
 });
}
TDG
  • 1,294
  • 4
  • 18
  • 50