I am trying to build external popup code with jQuery. I have used below code to do the same.
The problem is if I have not added specified class external to the anchor it will not open the popup.
Is there any alternative for the below or trick to do this?
$("a").on('click', function (e) {
if ($(this).hasClass('external')) {
e.preventDefault();
var href_val = $(this).attr('href');
$('.popupHolder').show();
$('.popupHolder h3 span').html(href_val);
}
});
$("button.proceed").on('click', function (e) {
window.location.href = $('.popupHolder h3 span').html();
});
$("button.cancel").on('click', function (e) {
$('.popupHolder').hide();
});
.popupHolder {
display: none;
position:fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
background-color: #fff;
z-index: 999;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">
<h1 id="back">Home</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's
standard
dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
specimen
book. <a class="external" href="https://www.google.com/">Google.com</a> <a href="#about">Hash Link About</a>
</p>
</div>
<div class="content">
<h2 id="about">About</h2>
<p>Lorem Ipsum is simply dummy text of the <a class="external" href="https://www.facebook.com/">Facebook.com</a>
printing and
typesetting industry. Lorem Ipsum has been the industry's
standard
<a href="#back">Back to Top</a>
</p>
</div>
<div class="content">
<p><a href="https://www.twitter.com/">Twitter.com</a>
<a href="#back">Back to Top</a>
</p>
</div>
<div class="popupHolder">
<h3>Do you want to visit <span></span>?</h3>
<button class="proceed">Proceed</button>
<button class="cancel">Cancel</button>
</div>
2
Answers
You have added the condition in the script that if element has
external
class then only popup will be shownjust remove that condition
Here you need to check the current href value is with same host. If that so then it will show the popup as per required.
Else it will show directly opens the url without appearing the popup.
Try below Code