If the user goes to a specific page on a website from another page, I want a back button to appear. If someone just typed in the url of that page, it shouldn't appear. How can I do that with js? I tried setting a variable on one page that is true if I navigated, but the problem is that I can't read the value of the variable on the other page.
如果用户从另一个页面转到网站上的特定页面,我想要一个后退按钮。如果有人刚刚输入该页面的网址,则不应显示该网页。我怎么能用js做到这一点?我尝试在一个页面上设置一个变量,如果我导航的话,这是真的,但问题是我无法在另一个页面上读取变量的值。
4 个解决方案
#1
4
document.referrer
gives you the URL (if any) of the previous page.
document.referrer为您提供上一页的URL(如果有)。
In general, if you are trying to pass information from page to page, see window.localStorage.
通常,如果您尝试将信息从一个页面传递到另一个页面,请参阅window.localStorage。
#2
0
Perhaps using window.referrer attribute can help solve your issue. In case you just typed in that url, this attribute will be undefined. Otherwise, it will contain the url of the page that sent you to your new page.
也许使用window.referrer属性可以帮助您解决问题。如果您只输入该URL,则此属性将是未定义的。否则,它将包含将您发送到新页面的页面的网址。
#3
0
What you are looking for is the referrer, which you can get using document.referrer
.
你要找的是引用者,你可以使用document.referrer获得。
Then, just check the referrer's domain name matches yours using a regex.
然后,使用正则表达式检查引荐来源的域名是否与您的域名相匹配。
#4
0
It's a little complex than other answers, but it can help you.
它比其他答案有点复杂,但它可以帮助你。
You can use local Storage or local session - sometimes working with cookie is difficult. On button click add in local storage current URL. Then on the next page you can check for the value exists
您可以使用本地存储或本地会话 - 有时使用cookie很困难。在按钮上单击添加本地存储当前URL。然后在下一页上,您可以检查值是否存在
if (!!localStorage["Url"]) {
// You have a prev page URL
}
$('body').on('click', 'a', function(){
localStorage["Url"] = document.location;
})
#1
4
document.referrer
gives you the URL (if any) of the previous page.
document.referrer为您提供上一页的URL(如果有)。
In general, if you are trying to pass information from page to page, see window.localStorage.
通常,如果您尝试将信息从一个页面传递到另一个页面,请参阅window.localStorage。
#2
0
Perhaps using window.referrer attribute can help solve your issue. In case you just typed in that url, this attribute will be undefined. Otherwise, it will contain the url of the page that sent you to your new page.
也许使用window.referrer属性可以帮助您解决问题。如果您只输入该URL,则此属性将是未定义的。否则,它将包含将您发送到新页面的页面的网址。
#3
0
What you are looking for is the referrer, which you can get using document.referrer
.
你要找的是引用者,你可以使用document.referrer获得。
Then, just check the referrer's domain name matches yours using a regex.
然后,使用正则表达式检查引荐来源的域名是否与您的域名相匹配。
#4
0
It's a little complex than other answers, but it can help you.
它比其他答案有点复杂,但它可以帮助你。
You can use local Storage or local session - sometimes working with cookie is difficult. On button click add in local storage current URL. Then on the next page you can check for the value exists
您可以使用本地存储或本地会话 - 有时使用cookie很困难。在按钮上单击添加本地存储当前URL。然后在下一页上,您可以检查值是否存在
if (!!localStorage["Url"]) {
// You have a prev page URL
}
$('body').on('click', 'a', function(){
localStorage["Url"] = document.location;
})