How to stop running the code if the user made a specific action

Hi !

I have set an API to pop up a lightbox if the user is not coming from Canada. This is working great. The lightbox is asking the user if still wants to stay on this website or be redirected to the US site.

The problem I have is that if the user chooses to stay on the site, by clicking the button6, the code on the master page is running each time the user is changing the page, and the pop-up comes up. That’s annoying. Someone here gave me the hint to introduce a “session variable” and verify if the user had clicked the button6 before running the code…

Here is how I have introduced the solution :

Code on lightbox page :

import wixWindow from 'wix-window';

$w.onReady(function () {

$w('#button6').onClick(function () {
wixWindow.lightbox.close("Website Redirect");

})

})

Code on the masterPage :

import {fetch} from 'wix-fetch';
import wixWindow from 'wix-window';
import {session} from "wix-storage";

$w("#button6").onClick(function() {
    session.setItem("userConfirm", true)
    wixWindow.lightbox.close("Website Redirect");
}

$w.onReady(function () {
let userConfirm = session.getItem("userConfirm")

if(userConfirm !== true) {

 // Fetching the user's location details
     fetch('https://ipapi . co/json', {
        method: 'get'
     })
 // Check if the request was successful
     .then((httpResponse) => {
 if (httpResponse.ok) {
 return httpResponse.json();
        }
     })
 
     .then((json) => {
 // Set the user's countryCode as a const
 const loc = json.country_code;
 
 /* Check if country code is of Canada */
 if(loc !== "CA"){
    wixWindow.openLightbox("Website Redirect");
    }
     });
}})

However, this is not right, I’m facing this error :

public/pages/masterPage.js: Unexpected token, expected “,” (12:0)
10 | }
11 | >
12 | $w.onReady(function () {
| ^
13 |
14 | let userConfirm = session.getItem(“userConfirm”);
15 |

It’s probably a problem in my syntax, but I don’t figure it out… Anyone can help? :slight_smile:

Note: I had to break the ipapi URL to post this.

Thank you so much!

It is not clear what code you have in the lightbox code and what code you have in the main page (or masterPage?).
Please separate the codes (and put each of them in a code block).

And a few comments:

  1. Make sure to have all the $w inside $w.onReady
  2. Use String and not Boolean as the value for the session method.

Thank you for your reply ! All the code on the initial post was from the main page (masterPage as called by Wix).

Here is the code on the lightbox page to close it by pressing button6:

import wixWindow from 'wix-window';

$w.onReady(function () {

$w('#button6').onClick(function () {
wixWindow.lightbox.close("Website Redirect");

})

})

Here is the code on the masterPage:

import {fetch} from 'wix-fetch';
import wixWindow from 'wix-window';
import {session} from "wix-storage";

$w.onReady(function () {

$w("#button6").onClick(function() {
    session.setItem("userConfirm", true)
    wixWindow.lightbox.close("Website Redirect");
}
    
let userConfirm = session.getItem("userConfirm")

if(userConfirm !== true) {

 // Fetching the user's location details
     fetch('https://ipapi . co/json', {
        method: 'get'
     })
 // Check if the request was successful
     .then((httpResponse) => {
 if (httpResponse.ok) {
 return httpResponse.json();
        }
     })
 
     .then((json) => {
 // Set the user's countryCode as a const
 const loc = json.country_code;
 
 /* Check if country code is of Canada */
 if(loc !== "CA"){
    wixWindow.openLightbox("Website Redirect");
    }
     });
}})

I moved the $w.onClick function after “onReady” but still have the same error.

I’m learning on the go, but I really don’t know what you mean by using String instead of Boolean… Could you tell me more?

Thank you so much for your help!

@y-dorey

  1. I don;'t understand why you put “Website Redirect” in the lightbox.close() call.

  2. This part (on the masterPage ) in not clear:

    $w("#button6").onClick(function() {
        session.setItem("userConfirm", true)
        wixWindow.lightbox.close("Website Redirect");
    }

Button 6 is on the lightbox and the event handler should opnly be in the lightbox code.
3. You should set the session item in the lightbox code.
4. it should be session.setItem(“userConfirm”, “true”);
5. masertPage: It should be if(!userConfirm){

@jonatandor35 THANK YOU SO MUCH!!! Problem solved !! :smiley:

“Website Redirect” is the name of the lightbox.

Here is my final code, which is working perfectly:

Lightbox code:

import wixWindow from 'wix-window';
import {session} from "wix-storage";

$w.onReady(function () {

$w('#button6').onClick(function () {
session.setItem("userConfirm", "true");
wixWindow.lightbox.close("Website Redirect");

})

})

MasterPage code:

import {fetch} from 'wix-fetch';
import wixWindow from 'wix-window';
import {session} from "wix-storage";

$w.onReady(function () {
    
let userConfirm = session.getItem("userConfirm")

if(!userConfirm) {

 // Fetching the user's location details
     fetch('https://ipapi . co/json', {
        method: 'get'
     })
 // Check if the request was successful
     .then((httpResponse) => {
 if (httpResponse.ok) {
 return httpResponse.json();
        }
     })
 
     .then((json) => {
 // Set the user's countryCode as a const
 const loc = json.country_code;
 
 /* Check if country code is of Canada */
 if(loc !== "CA"){
    wixWindow.openLightbox("Website Redirect");
    }
     });
}})

Thank you for your time, it’s much appreciated ! :slight_smile: