Я использую этот код в своем файле wordpress functions.php, чтобы автоматически добавлять nofollow к каждой ссылке определенного URL:
function cdx_handel_external_links() {
?>
<script type = "text/javascript">
( function( $ ) {
$("a[href^=http]").click(function(){
if (this.href.indexOf(location.hostname) == -1) {
$(this).attr({
target: "_blank"
});
}
})
//Add Nofollow
$("a").each(function(){
if (this.href.indexOf('example-url.com') >0 ){
$(this).attr({
rel: "nofollow"
});
}
});
} )( jQuery );
</script>
<?php
}
add_filter( 'wp_footer', 'cdx_handel_external_links', 999);
Но как я могу добавить в этот код новые URL-адреса? (Я новичок в программировании, сэр). Может быть через:
indexOf('example-url.com','example-url2.com','example-url3.com')
? Большое спасибо за вашу помощь!
Обновлено:
Я сделал это как нуб, и у меня это сработало:
function cdx_handel_external_links() {
?>
<script type = "text/javascript">
( function( $ ) {
$("a[href^=http]").click(function(){
if (this.href.indexOf(location.hostname) == -1) {
$(this).attr({
target: "_blank"
});
}
})
//Add Nofollow
$("a").each(function(){
if (this.href.indexOf('example-url.com') >0 ){
$(this).attr({
rel: "nofollow"
});
}
});
//Add Nofollow 2.domain
$("a").each(function(){
if (this.href.indexOf('example-url2.com') >0 ){
$(this).attr({
rel: "nofollow"
});
}
});
//Add Nofollow 3.domain
$("a").each(function(){
if (this.href.indexOf('example-url3.com') >0 ){
$(this).attr({
rel: "nofollow"
});
}
});
} )( jQuery );
</script>
<?php
}
add_filter( 'wp_footer', 'cdx_handel_external_links', 999);
какое заявление?
Ну должен был быть "Оператор ИЛИ" developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…



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


Вы можете использовать Array.prototype.includes ():
const urls = ['www.example-url.com', 'www.example-url-1.com', 'www.example-url-2.com'];
$('a').each(function() {
if ( urls.filter(url => url.includes('example-url.com')).length > 0 ) {
$(this).attr({
rel: 'nofollow',
});
}
});Не будет работать, если это частичное совпадение полного URL
some () на мой взгляд будет лучше, так как он остановится при первом ударе, но фильтр будет работать.
Мы хотим знать, содержит ли строка href одну из запрещенных подстрок.
let nonFollow = ['example-url.com','example-url2.com','example-url3.com'];
function isNonFollow(anHref) {
for (i=0; i < nonFollow.length; i++) {
let href = nonFollow[i];
if (anHref.indexOf(href) >0) return true;
});
return false;
}
$("a").each(function(){
if (isNonFollow(this.href)){
$(this).attr({
rel: "nofollow"
});
}
});
или заявление ....