[英]Excluding tel and mailto in base target statement
根據客戶的要求,我們強制所有外部鏈接在新選項卡中打開。 目前,我正在為此使用js腳本。
但是,這也迫使tel和mailto鏈接也要打開新選項卡。 我認為這就是為什么當您單擊並發送電子郵件鏈接時,在某些設備上出現錯誤的原因,其中一個彈出框顯示“此網站已被禁止自動撰寫電子郵件”-在iPhone 6上專門發現了該電子郵件。
如何強制外部鏈接-但不包括郵件和電話鏈接? 非常感謝您的協助。
這是我當前正在使用的外部鏈接腳本。 我以為也許我可以做些改動,只查找帶有“ http”但帶有tel和mailto的else語句的外部鏈接。 但是我不確定該怎么做。
jQuery(document).ready(function($) {
$('a').each(function() {
var a = new RegExp('/' + window.location.host + '/');
if(!a.test(this.href)) {
$(this).click(function(event) {
event.preventDefault();
event.stopPropagation();
window.open(this.href, '_blank');
});
}
});
});
結合我的評論,我將選擇檢查每個錨點的href
屬性,並檢查它是否不以mailto:
或tel:
開頭(並且外部鏈接根本不起作用,我想這是由代碼段環境引起)
jQuery(document).ready(function($) { $('a').each(function() { var a = new RegExp('/' + window.location.host + '/'); if(!a.test(this.href) && this.href.substr(0,4)!='tel:' && this.href.substr(0,7)!='mailto:') { $(this).attr('target', '_blank'); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="http://www.google.com">website</a>, <a href="mailto:someone@somewhere.com">e-mail</a> and <a href="tel:123456789">phone</a>.
我必須承認,檢查您可能想排除的所有協議並不是理想的選擇,但絕對比檢查您希望在新窗口/選項卡中打開的所有內容容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.