Я пытаюсь создать внешний всплывающий код с помощью jQuery. Я использовал приведенный ниже код, чтобы сделать то же самое.
Проблема в том, что если я не добавил указанный внешний класс к якорю, он не откроет всплывающее окно.
Есть ли альтернатива приведенному ниже или трюк, чтобы сделать это?
$("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>


![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Вы добавили в скрипт условие, что если элемент имеет класс external, то будет отображаться только всплывающее окно.
просто убери это условие
$("a").on('click', function (e) {
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();
});
Здесь вам нужно проверить, что текущее значение href относится к тому же хосту. Если это так, то всплывающее окно будет отображаться в соответствии с требованиями.
В противном случае он будет показывать прямое открытие URL-адреса без появления всплывающего окна.
$(document).on('click', 'a', function (e) {
var a_id = $(this).attr('id');
var href_val = $(this).attr('href');
if (href_val[0] != '#' && href_val != undefined) {
var j = href_val.indexOf("://");
var host = "";
for (i = j + 3; i < href_val.length; i++) {
if (href_val.charAt(i) != '/') {
host = host + "" + href_val.charAt(i);
} else {
break;
}
}
var my_url = window.location.hostname;
if (my_url != host) {
if (a_id == undefined) {
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>Спасибо @Mahesh Thorat, это сработало отлично.
Однако это откроет все ссылки на странице в виде всплывающего окна, что, безусловно, нежелательно.