IE and Edge sending encoded URL to the server

584 Views Asked by At

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.

0

There are 0 best solutions below