0

I am trying to send some parameters from a booking form to an online booking company's website. The idea is to get a quick redirection for the visitor to the website with a date and a date plus one day (in JavaScript) for the booking as a starting point.

Everything works fine in Chrome & Firefox. In IE and Edge though the tail end date is sent with encoding thus:

https://xxxxxxx.com/xxxxxx/?startdate=2018-02-28&adults1=1&children1=0&enddate=2018-3-01

Which works fine from Chrome etc. Once this has been generated in IE or Edge though it does not work. It is sending this to the server:

https://xxxxxxx.com/xxxxxx/?startdate=2018-02-28&adults1=1&children1=0&enddate=%E2%80%8E2018-%E2%80%8E03%E2%80%8E-%E2%80%8E01%E2%80%8E

jQuery(document).ready(function () {
  jQuery('#book-room').click(function () {
    var term = jQuery('#fromdate').val();
    var dateAr = term.split('/');
    var newFromDate = dateAr[2] + '-' + dateAr[1] + '-' + dateAr[0].slice(-2);
    // Plus One Date:
    var myDate = new Date(newFromDate);
    myDate.setDate(myDate.getDate(term) + 1);
    var plusOneDate = myDate.toLocaleString( 'en-GB', { year: 'numeric', month : 'numeric', day : '2-digit' } );
    var newDay = plusOneDate.replace(/\//g, '-');
    var dateAr2 = newDay.split('-');
    var newPlusOneDate = dateAr2[2] + '-' + dateAr2[1] + '-' + dateAr2[0].substr(0); 
    window.open(decodeURIComponent("https://xxxxxxx.com/xxxxxx/?startdate=" + newFromDate + "&adults1=" + newAdults + "&children1=" + newChildren + "&enddate=" + newPlusOneDate), "_blank");
  });
})

The newPlusOneDate is the variable that is causing all the trouble. It should be in the format 2018-3-01 which is required at the other end. The form date works fine everywhere.

The only way I could get the date + one day (newPlusOneDate) variable to be formatted correctly was using that rather long-winded toLocaleString() function and chopping it up. there must be an easier way.

I have tried to use decodeURIComponent and decodeURI too to rid myself of the encoding but without success.

Any suggestions on how to get the last date string correctly formatted without the encoding would be extremely welcome.

Joe
  • 41,484
  • 20
  • 104
  • 125
Alperian
  • 119
  • 1
  • 2
  • 8
  • Why are you doing `decodeURIComponent`? Other than that I don't see anything that could mess with URI encoding. Can you check each step of building the string to find where the URI encoding appears? – Halcyon Jan 03 '18 at 18:37
  • 1
    I think you should try to changing the way to build `newPlusOneDate`. It can be `var newPlusOneDate = myDate.getFullYear() + "-" + (myDate.getMonth()+1) + "-" + myDate.getDay();` – StupidDev Jan 03 '18 at 18:45
  • doesn't urlencode work? you should url encode each parameter value and concat the whole URL with all parameters, something like http://yourdomain.com/xxxxx?param1=encodeURIComponent(value1)&param2=encodeURIComponent(value2) – Jim Jan 03 '18 at 18:53

0 Answers0