This code is defined on my Site tab. NOTE the variable previousPageURL
I want to access this variable from code in one of my pages. However, it says previousPageUrl is undefined. So, how to I access this variable from a page?
This code is defined on my Site tab. NOTE the variable previousPageURL
I want to access this variable from code in one of my pages. However, it says previousPageUrl is undefined. So, how to I access this variable from a page?
Note: it should be wixLocation.to NOT wixLocation.url. But, as you see below, the result is the same – still not defined error.
Hello Imacklin,
well first at all, please do not use pics to show your CODE!
It is better for all to have the CODE inside a “CODE-TAG”, which you will find here…
I am not sure, but i think you have a failure in your “onReady”-code part.
You do GET before you SET!
First SET —> then ----> GET !
$w.onReady(function(){
session.setItem("page", wixLocation.url);
previousPageURL = session.getItem("page");
});
I think this will cause the error in your code.
The result is the same (undefined). What I am really looking for is the Wix equivalent of window.onload and window.onbeforeunload. When leaving a page, the session,setItem(“page”, winLocation.url) function would be placed in the window,onbeforeunload event handler, Then. in the page that was just navigated to, the previousPageURL = session.getItem(“page”) function would be placed in the window,onload event handler.
Basically, you set the current url upon leaving any page, then retrieve this value upon entering any new page. But the wix-window api doesn’t have onload() or onbeforeunload() functions. Of course you could write code in every page setting and getting session variables, but I was hoping to get this working by writing the code once and putting it in the Site tab. But I can’t seem to get this to work,
Well, I gave up trying to get code on the Site tab to work. Instead, I created a pageNavigation.js file in the /public folder with currentPage() and previousPage() functions (which set and get the session variables. Then I simply invoke these functions on every page where I need to know how the user got to that page. This works.
In case anyone is interested, here is my reengineered code THAT WORKS.
/public/pageNavigation.js
import {session} from 'wix-storage';
import wixLocation from 'wix-location';
export function currentPage() {
session.setItem("page", wixLocation.url);
}
export function previousPage() {
return session.getItem("page");
}
In the page navigating FROM
import {currentPage} from 'public/pageNavigation.js';
import {previousPage} from 'public/pageNavigation.js';
import {session} from 'wix-storage';
$w.onReady(function () {
// NOTE: if navigating from Members Only page to SVCGR File Share page, then currentPage MUST
// be set in the Members Only page
// Set this page URL in the public/pageNavigation.js shared module
currentPage();
In the page navigated TO
import {currentPage} from 'public/pageNavigation.js';
import {previousPage} from 'public/pageNavigation.js';
$w.onReady(function () {
// NOTE: if navigating from Members Only page to SVCGR File Share page, then currentPage MUST
// be set in the Members Only page
let prevPage = previousPage();
Ok, THX. Will also take a look to it tomorrow.
Smart! Thanks for sharing this so that others can reuse it.
Thanks so much for sharing this! I had the exact same issue when I looked at the wix example!
Has anyone figured this out yet? It should be quite simple, but it seems a lot of people have the question and nobody is able to give a working solution.
Basically, I have a form and I want field #input6 to take the URL of the previous page when I submit the form. I have 2 snippets of code - 1 from the Public Site and 1 from the page with the form.
Global site: ‘pageNavigation.js’
import { session } from ‘wix-storage’ ;
import wixLocation from ‘wix-location’ ;
let previousPageURL ;
$w . onReady ( function () {
previousPageURL = session . getItem ( “page” );
session . setItem ( “page” , wixLocation . url );
});
Page: With the Form
import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;
$w . onReady ( function () {
let url = wixLocation . url ;
$w ( “#input6” ). value = url ; // Line 6
});
The issue I have is that when I try to change ‘url’ in Line 6 to ‘previousPageURL’, I get a ‘cannot find field name’ error.
You are getting the “cannot find field name” error, because you do not have previousPageURL defined on the page with the form. You need to first retrieve the url of the previous page from storage, and you can then use it to set the input element on the page. You need something like this on the page with the form:
$w.onReady(function() {
let previousPageURL = session.getItem("page");
$w("#input6").value = previousPageURL;
});
Thanks Yisrael / Brewmaster. How do I retrieve the url from storage?
When I update the page code to what you see below, the value inputted into field #input6 is the current webpage and not the previous one.
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import {local, session, memory} from 'wix-storage';
$w.onReady(function() {
let previousPageURL = session.getItem("page");
$w("#input6").value = previousPageURL;
});
The “other” page (or pages) needs to save its own URL like this:
session.setItem("page", wixLocation.url);
Then, when the page with the form is run, it retrieves the url like this:
let previousPageURL = session.getItem("page");