[英]Modal doesn't show if user manually types in our url in address bar
當用戶從外部鏈接(即 Google 搜索結果)訪問我們的頁面時,我創建的模態顯示,但如果用戶通過在地址欄中手動輸入我們的 URL 進入我們的網站,則會發生錯誤並且模態不會出現。
這是我的代碼:
const siteUrl = ["website.com"];
const referrer_hostname = new URL(document.referrer).hostname;
if (siteUrl.includes(referrer_hostname)) {
console.log("Don't Show Modal", document.referrer);
} else {
console.log("Show Modal", document.referrer);
$( window ).on('load', function() {
console.log("closure modal firing");
$('#closureModal').modal({
backdrop: 'static',
keyboard: false,
show: true
});
});
#closureModal
連接到模態的 HTML。
錯誤:( (index):123 Uncaught TypeError: Failed to construct 'URL': Invalid URL
當您在地址欄中輸入 URL 時,document.referrer 是一個空字符串“”。 嘗試添加驗證:
const referrer_hostname = document.referrer !== "" ? new URL(document.referrer).hostname : "";
這使您可以避免 TypeError
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.