[英]Preloader - how to turn it off when returning from the subpage to the main page?
[英]Way to check were you redirected either from a main page or subpage
我嘗試僅當用戶從主頁而不是子頁面重定向時執行javascript動畫。
我以為document.referrer.match()可以做到這一點,但是我可能已經弄亂了match()參數。
另外,我也在本地服務器上運行它,而iglakowe.com是主頁地址。
const ref = document.referrer.match(/iglakowe.com/) if (ref != null){ document.getElementById("bg").style.opacity = 0; }
您可以通過一些字符串操作來做到這一點。 首先獲取文檔引薦來源
const ref = document.referrer;
這可能會返回類似https://stackoverflow.com/somePage.html的內容
從返回的字符串中刪除//
const ref = document.referrer.replace("//","");
裁判將是https:stackoverflow.com/somePage.html
現在只需搜索/ char的出現
const ref = document.referrer.replace("//","").indexOf("/");
ref現在是一個正整數,因為字符串內有一個/ ,這實際上意味着/跟隨着文件夾或文件名之類的東西。
如果為-1,則幾乎可以肯定地說引薦來源網址是主頁。
const ref = document.referrer.replace("//","").indexOf("/");
if (ref>-1){
document.getElementById("bg").style.opacity = 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.