Trigger Event With URL

Could really use some help. I need some strips to collapse when a page is accessed by visitors that are given an extended page URL. Wix-http-functions seemed like the way to do that, so I made a back-end .js file called ‘http-functions.js’ and entered the following back-end code copied right out of the wix-http-functions page. I then added the front-end code below to the page. The extended link, https://gregkulda.wixsite.com/test/home/myURLfunction/info , takes me to the page, but doesn’t collapse the strips.

Would greatly appreciate anyone who can show me how to make this work. See link to my test editor at bottom.

BACK-END CODE:
// http-functions.js
import {ok, notFound} from ‘wix-http-functions’;

// URL looks like:
// https://www.mysite.com/_functions/myFunction/findMe
// or:
// https://user.wixsite.com/mysite/_functions/myFunction/findMe
export function get_myURLfunction(request) {
if (request.path[0] === “info”) {
const body = “Found it!”;
return ok({body: body});
}
const body = “Didn’t find it!”;
return notFound({body: body});
}

FRONT-END PAGE CODE:
import {ok, notFound} from ‘wix-http-functions’;

export function get_myURLfunction(request) {
if (get_myURLfunction(request)) {
const body = “Found it!”;
$w(‘#columnStrip2’).collapse();
$w(‘#columnStrip3’).collapse();
}
const body = “Didn’t find it!”;
return notFound({ body: body });
}

LINK TO EDITOR:
https://editor.wix.com/html/editor/web/renderer/edit/bda7405d-45fd-49b7-8fa9-ca8f60282775?editorSessionId=cf47568a-e5dc-4e11-9507-ef40ba6715b7&metaSiteId=d8aa75ab-a164-47ef-bc63-96f243dc073d

Hey,

Thanks for the question. First of all, be aware that wix-http-functions works only from backend, so it doesn’t interact with your frontend code and, probably, for that reason it doesn’t work.

The easiest way to solve this is to actually use wix-location module (wix-location-frontend - Velo API Reference - Wix.com) to detect url directly in frontend, but using onReady handler (Wix Editor Elements ($w) - Velo API Reference - Wix.com), so it’s executed once page is loaded.

Let me know, if you need further help!

Giedrius,

WOW! Thank you for pointing me in the right direction. Big help!

Using Wix-Location as shown below, all I had to do is extend the URL with any string beginning with a ‘?’ to trigger special events as page loads. Both the single condition and multiple conditions sets of code below worked very well for me. If just site or page URL is entered (not extended version), page simply loads as designed. No need to put in code.

SINGLE CONDITION:
$w.onReady( function () {
let url = wixLocation.url;
if (url === “https://gregkulda.wixsite.com/testsite/testpage?todo1”) {
$w(‘#columnStrip1’).expand();
$w(‘#columnStrip2’).collapse();
$w(‘#columnStrip3’).collapse();
}
});

MULTIPLE CONDITIONS:
$w.onReady( function () {
let url = wixLocation.url;
if (url === “https://gregkulda.wixsite.com/testsite/testpage?todo1”) {
$w(‘#columnStrip1’).expand();
$w(‘#columnStrip2’).collapse();
$w(‘#columnStrip3’).collapse();
}
if (url === “https://gregkulda.wixsite.com/testsite/testpage?todo2”) {
$w(‘#columnStrip1’).collapse();
$w(‘#columnStrip2’).expand();
$w(‘#columnStrip3’).collapse();
}
if (url === “https://gregkulda.wixsite.com/testsite/testpage?todo3”) {
$w(‘#columnStrip1’).collapse();
$w(‘#columnStrip2’).expand();
$w(‘#columnStrip3’).expand();
}
});

Great! Glad that it helped!