Display a “Leaving Website” page when a user is leaving our domain | Community
Skip to main content
Level 2
February 13, 2024
Solved

Display a “Leaving Website” page when a user is leaving our domain

  • February 13, 2024
  • 2 replies
  • 642 views

In our AEM website, we want to display a “Leaving Website” page when a user is leaving our domain.  What would be the recommended way to code this?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SureshDhulipudi

You can achieve this by using JavaScript to detect when a user clicks on

any Button

any Anchor / Navigation link

CTA button click 

closing the browser window -- that leads to a different domain.

except closing the window - remaining three has configured with new domain URL.

you can write a Javascript

 

sample 

 

document.querySelectorAll('a').forEach(function(link) {
link.addEventListener('click', function(event) {
var linkhasOtherDomain = link.hostname;
var currentDomain = window.location.hostname;

if (linkhasOtherDomain !== currentDomain) {
event.preventDefault();
var userConfirmation = confirm('You are leaving our website. Are you sure you want to continue?');
if (userConfirmation) {
window.location.href = link.href;
}
}
});
});

 

for window close event - write a different condition.

2 replies

SureshDhulipudi
Community Advisor
SureshDhulipudiCommunity AdvisorAccepted solution
Community Advisor
February 13, 2024

You can achieve this by using JavaScript to detect when a user clicks on

any Button

any Anchor / Navigation link

CTA button click 

closing the browser window -- that leads to a different domain.

except closing the window - remaining three has configured with new domain URL.

you can write a Javascript

 

sample 

 

document.querySelectorAll('a').forEach(function(link) {
link.addEventListener('click', function(event) {
var linkhasOtherDomain = link.hostname;
var currentDomain = window.location.hostname;

if (linkhasOtherDomain !== currentDomain) {
event.preventDefault();
var userConfirmation = confirm('You are leaving our website. Are you sure you want to continue?');
if (userConfirmation) {
window.location.href = link.href;
}
}
});
});

 

for window close event - write a different condition.

Imran Khan
Community Advisor
Community Advisor
February 14, 2024