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…
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:
Make sure to have all the $w inside $w.onReady
Use String and not Boolean as the value for the session method.
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 !!
“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");
}
});
}})