Expand my Community achievements bar.

SOLVED

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

Avatar

Level 1

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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.