1

I am trying to redirect to another server call but unfortunately browser sometimes redirect or sometime just refresh the current page. Redirect is in getCheckOutOfHrs async function and I tried all three options

1). window.location = myurl;
2). window.location.href = myurl;
3). window.location.assing(myurl);
4). window.location.replace(myurl);

Nothing is 100% a working solution in my case. sometimes browser redirects and sometimes not.

I don't know why browser is doing this. see my tried code is below. I am trying to use async function.

$('#btn-search-car').click(function(e) {
  searchCar(e)
});

function searchCar(e) {
  try {
    //debugger;
    e.preventDefault();

    var obj = {
      Branch: lStrLocNam.trim(),
      PickupLocId: lIntPickupLocId,
      FranchiseId: lIntFranchiseId,
      SubofficeId: lIntSubofficeId,
      StartDate: dtStart /*startDate*/ ,
      StartTime: startTime,
      EndDate: dtEnd /*endDate*/ ,
      EndTime: endTime,
      VehicleType: selectedVehType,
      VehicleCategoryId: carCat,
      IsNewSearch: true
    };
    saveElement('SelectedParamInSearch', obj);
    let apiUrl = REACT_APP_CheckOutOfHours;
    getCheckOutOfHrs(obj);
  } catch (err) {
    //hideProgress();
  }

};





const getCheckOutOfHrs = async(obj) => {
  let abortController = new AbortController();
  let lattitude = 0;
  let longitude = 0;
  let resultPages = [];
  let apiUrl = REACT_APP_CheckOutOfHours;
  const settings = {
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify(obj),
    signal: abortController.signal
  };
  const responseDetail = await fetch(apiUrl, settings);
  //abortController.abort();
  if (responseDetail && !responseDetail.ok) {
    resultPages = [];

  } else {
    resultPages = await responseDetail.json();
  }

  try {
    let OutOfHours = resultPages; //? JSON.parse(resultPages):'';
    //debugger;           
    debugger;
    if (OutOfHours) {
      saveElement('OutOfHoursParam', OutOfHours);
      //OutOfHours = OutOfHours.replace('"', '').replace('"', '');
      if (OutOfHours.length != 0 && OutOfHours.item2 && OutOfHours.item2.indexOf("£") > 0) {
        //window.location = "/Home/FindBranch/?franchiseId=" + obj.FranchiseId; 
        let redirectLocalUrl = "/Home/FindBranch/?franchiseId=" + obj.FranchiseId;
        window.location.replace(redirectLocalUrl);
        // window.location.href = redirectLocalUrl;
        //  windowtruelocation.assign(redirectLocalUrl);
        return false;

        /*   $.ajax({
               url: "/Home/FindBranch",
               data: obj,
               success: function (data) {                       
                   window.location.href = "/Home/FindBranch";                                
               },
               dataType: "html",
               type: "POST",
               cache: false,
               error: function () {                                
               }
           }); */


      } else if (OutOfHours.length != 0 && OutOfHours.item2 && OutOfHours.item2.indexOf("£") < 0) {
        //OutOfHours = OutOfHours.replace("\\", "'").replace("\\", "");                     
        alert(OutOfHours.item2);
        return false;
      } else {
        //FindBranch
        //window.location = "/Home/FindBranch/?franchiseId=" + obj.FranchiseId;  
        let redirectLocalUrl = "/Home/FindBranch/?franchiseId=" + obj.FranchiseId;
        window.location.replace(redirectLocalUrl);
        // window.location.href = redirectLocalUrl;
        // window.location.assign(redirectLocalUrl);
        return true;

        /* $.ajax({
             url: "/Home/FindBranch",
             data: obj,
             success: function (data) {
                 // debugger;
                 window.location.href = "/Home/FindBranch";
                 //hideProgress();
             },
             dataType: "html",
             type: "POST",
             cache: false,
             error: function () {
                 // hideProgress();
             }
         }); */
      }
    }
  } catch (error) {
    debugger;

  }
};

Help is appreciated.

phuzi
  • 12,078
  • 3
  • 26
  • 50
maifs
  • 1,061
  • 6
  • 15
  • 31
  • Can you indent your code properly? – trincot Jun 13 '22 at 15:18
  • Code formatted! – phuzi Jun 13 '22 at 19:39
  • "*I don't know why browser is doing this.*" What have you leveraged within your browser's developer tools to debug this unexpected behavior? Off the bat, I would expect to see some sort of analysis having already been done by setting a [breakpoint on any change to the window's `location` property](https://stackoverflow.com/questions/12360187/break-javascript-before-an-inline-javascript-redirect-in-chrome) and understanding where that's coming from and what logic it's being driven by. – esqew Jun 13 '22 at 19:42

0 Answers0