Check if URL Parameter Exists

How do I check using Wix Code that any parameters at the end of a url exist? For example, this function would return true if the url looks like this:
https://mysite.com/page?parameter=1234
And would return false if the url looks like this:
https://mysite.com/page

I’ve been using wixLocation for my URL querying, but can’t seem to find any functions/properties that perform the task above. Any ideas how to perform this either through wixLocation or vanilla JavaScript?

https://www.wix.com/velo/reference/wix-location/queryparams-obj

Then just check the object if the key/value pair is there.

Ah, so wixLocation.queryParams.add doesn’t add a parameter if there are none to begin with?

No, you do not need to use .add or anything, just retrieve the query-part of the URL. Look here: https://www.wix.com/velo/reference/wix-location/query

So

let query = wixLocation.query;

returns on object with all the queryparams in it (key/value).

I see, to be more clear, I’m wanting to check if any parameters exist at the end of the URL. If there are no parameters, then the check would return false, if there are any parameters, it would return true.

I figured out a way to do this, not sure if it’s the most elegant solution, but you can use

if (wixLocation.url === "https://mysite.com/page/") {
    // true
} else {
    // false
}

Still hoping for a better solution just in case I want to use anchors (https://mysite.com/page#anchor1)

How about:

let query = wixLocation.query;
if (query){
//do something
}
else {
//do something else
}

@giri-zano Tried that and didn’t work, because wixLocation.query always returns something regardless of whether there is a query or not. It returns a big list of properties that I have no idea what to do with :sweat_smile:

@daniel48582 Never tried that, but what is inside that object if you JSON.stringify() it?

@giri-zano

{
  "configName": "responsive",
  "ds": "true",
  "dsOrigin": "editor_x",
  "editorSessionId": "9f1c3e62-31d8-4338-97a5-8752a22fb7b3",
  "esi": "9f1c3e62-31d8-4338-97a5-8752a22fb7b3",
  "experiments": "bv_wixImagePhaseTwo,bv_wixDropdown,sv_renderIframeSrcInSsr",
  "experimentsoff": "bv_nativeComponentsInPreviewMode,bv_nativeComponents",
  "isEdited": "true",
  "isSantaEditor": "true",
  "metaSiteId": "f4d70cf7-d921-4563-9059-696bd4eb13ec",
  "petri_ovr": "specs.ExcludeSiteFromSsr:true;specs.UseWixBoltRenderer:true;specs.UseBoltInPreview:true;specs.pro-gallery.useNewAPIs:true;specs.UseIsPermittedAuthorization:true;specs.responsive-editor.CustomMenuFlow:true",
  "testParam": "true"
}

api info - depended on WIX " Free site " or " Premium site "
https://www.wix.com/velo/reference/wix-location/introduction#wix-location_introduction_premium-sites

this more logical with !==

//page name - prefix
if(wixLocation.prefix !== "PageName")
{
 //do stuff
}

//or query
if(wixLocation.query !== "name=MyQuery")
{
 //do stuff
}

note:
it should be works but it doesn’t works.