[英]Is there a way to ask if part of a domain is included in document.referrer?
我正在為我們的網站設置一個模式,我們希望它只在用戶從外部頁面(即從谷歌到我們的頁面)進入我們的任何頁面時出現。 模態應該顯示在所有頁面上,所以我將它設置在我們的頁眉中,因為這是我們網站中不變的兩個部分之一(另一個是頁腳)。 我有一個使用 document.referrer 條件的特定頁面的模態,但代碼設置為我必須逐字輸入我們網站(超過 500 頁)中的每個其他 url 才能使其工作。
所以問題是,有沒有一種方法可以在條件中定位域 URL,如果域包含在上一頁(引用者)url 中,則不顯示模態?
這是我現在擁有的模態示例:
const domains = ["http://website.com/*"];
if (domains.includes(document.referrer)) {
console.log("Don't Show Modal - from any park page", document.referrer);
} else {
console.log("Show Modal - From other Page", document.referrer);
$( window ).on('load', function() {
console.log("closure modal firing");
$('#closureModal').modal({
backdrop: 'static',
keyboard: false,
show: true
});
});
}
#closureModal
連接到模態 HTML 代碼。
您可以利用URL
接口來執行此比較。 您必須在domains
數組中使用主機名(即,沒有協議或路徑)或通過URL
處理domains
的條目,但無論哪種方式,它都應該非常強大,可以滿足您的需要。
const domains = ["stackoverflow.com"]; const referrer_hostname = new URL(document.referrer).hostname; if (domains.includes(referrer_hostname)) { console.log("Don't Show Modal - from any park page", document.referrer); } else { console.log("Show Modal - From other Page", document.referrer); /* modal manipulation */ }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.