Wix location query parameters

Hi folks,

I’ve been having some issues with trying to get multiple query parameters from a URL. If I just pass one it’s fine but if I add anymore it returns an empty object.

So this code works fine to pass

export function searchTrip_click(event) {
    wixLocation.to('/activity-search')
    wixLocation.queryParams.add({
        "cat": $w("#categoryDrpDwn").value,
    });
} 

And this work to get

$w.onReady(function (event) {
  let query = wixLocation.query;
  let cat = query.cat;
});

But as soon as I try to pass more than one (it populates the URL correctly)

export function searchTrip_click(event) {
    wixLocation.to('/activity-search')
    wixLocation.queryParams.add({
        "cat": $w("#categoryDrpDwn").value,
        "startDate": $w('#datePicker1').value
    });
}

//activity-search?cat=1&startDate=Wed+Oct+19+2022+00%3A00%3A00+GMT%2B0100+%28British+Summer+Time%29

But doesn’t show the data on the other page

$w.onReady(function (event) {
   let query = wixLocation.query;
   let cat = query.cat;
   let start = query.startDate;
   console.log(cat)
   console.log(start)
});

The console returns undefined.

Can someone let me know if I’m missing something obvious here?

You need to run the function after you change the params and no before.

@J.D. What an idiot I was! Thanks for helping my tired brain… :sleeping:

@J.D. I seem to have a new issue that I need to refresh the page to populate the data into the input fields on the new page. This is when the filter would run. It all works great but sometimes the page needs refreshed.


let query = wixLocation.query;
let cat = query.cat;
let start = query.startDate;
let end = query.endDate;


$w.onReady(function (event) {
   $w('#categoryBox').selectedIndices = [Number(cat)];
   $w('#startDate').value = new Date(start);
   $w('#endDate').value = new Date(end);
        initComps();
});

Any ideas?

I do’;t really know what you’re trying to do and what the problem you see.
Please add details and some longer description.

Hi JD,

Sorry for the delay, I’ve been working away.

Basically I’m just trying to add parameters to the URL, this works fine. Then use the parameters on another page which will populate fields. The issue is I need to reload the newly directed page to get it to work.

I.e. I have an item page, I want to transfer the data via URL to the payment page then use this data to populate data on the payment page. The URL populates, and redirects to the new page but upon loading all items are returned as undefined unless the page is reloaded. The code is as above.

let query = wixLocation.query;
let cat = query.cat;
let start = query.startDate;
let end = query.endDate;   

$w.onReady(function(event){
    console.log(cat);
    console.log(start);
    console.log(end);
});

I hope this makes a bit more sense and if you had any thoughts it would be very much appreciated.

Thanks, Stephen

Your code should work (if the params are in the url, and if you imported wixLocation on this page).
__
If it doesn’t, try moving everything into the $w.onReady block.

Hey JD,

I can’t get it to work as intended.

With some investigation work, it seems like the new page is loaded first then the parameters are added too late. This video should explain what I mean better.

The code doesn’t load but if you refresh it works, then if you press the back button on the browser it shows just the page URL without the parameters.

When I noticed this, I just copied and pasted the full URL with the parameters and the code loads perfectly fine…

Any ideas? Or do you think the redirect isn’t working properly on the wix side?

It’s not so easy to understand what’s going on just from watching this video or reading the description.
I think I saw the parameters there (in your video) from the beginning and not just after page load.
Maybe it has something to do with the booking app (I don’t use it, so I can’t tell)
Sorry I couldn’t help.

Thanks JD< I appreciate you taking the time to have a look. It seems to me like there is an issue with the way the parameters are being added to the URL but I’m not 100% sure.

I’ll try to touch base with Wix to see what they think.

Thanks again :slight_smile: