admin管理员组文章数量:1296392
I am currently building a site using React and GatsbyJS and trying to conditionally add a navigation window.history.back(-1);
if the previous page was part of the same domain.
An example is
navigateBack = () => {
if (window !== undefined) {
window.history.back(-1);
}
};
note that if (window !== undefined)
allows window
to be used without server side rendering.
How can I check the previous page before running this function.
For example is the user navigated from a external link I want to use navigate('/stories/')
, else if the user came from an internal page, i.e. '/stories/2/'
I want them to go back using window.history.back(-1);
I am currently building a site using React and GatsbyJS and trying to conditionally add a navigation window.history.back(-1);
if the previous page was part of the same domain.
An example is
navigateBack = () => {
if (window !== undefined) {
window.history.back(-1);
}
};
note that if (window !== undefined)
allows window
to be used without server side rendering.
How can I check the previous page before running this function.
For example is the user navigated from a external link I want to use navigate('/stories/')
, else if the user came from an internal page, i.e. '/stories/2/'
I want them to go back using window.history.back(-1);
5 Answers
Reset to default 4I've had to address this by checking to make sure there's a history to go to, and then by verifying that the current host can be found in the referrer. If those are found then I know they came from within my site, so I go back; otherwise, I navigate them elsewhere.
if(window.history.length > 1 &&
document.referrer.indexOf(window.location.host) !== -1) {
window.history.back();
}
else {
<do something else>
}
Gatsby has an api that'll tell you the previous location. Perhaps you can use it together with document.referrer
like @ATT suggested to check if previous url were from the same domain.
// gatsby-browser.js
exports.onRouteUpdate = ({ location, prevLocation }) => {
if (prevLocation || document.referrer.includes(SITE_NAME)) {
// same domain
}
}
Alternatively, if you want to have previous location information in your React ponent, check out the answers to this other gatsby question.
Side note: document.referrer
will be blank (""
) instead of null
or undefined
if there were no referrer (site omit referrer, or direct)
You can get the URL of the previous page by document.referrer
.
If you are using React Router, you can use the following snippet.
if(browserHistory.getCurrentLocation().key === null) {
browserHistory.push(customRoute); // push to custom route
} else {
browserHistory.go(-1); // go back normally
}
Answered here for a version that works also when the window is "new" and the referrer might not be reliable https://stackoverflow./a/68161306/1348517
本文标签:
版权声明:本文标题:javascript - Conditionally use window.history.back(-1) if previous page is not from the same domain - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741620577a2388784.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论