I am trying a user to redirect back to the blog he was reading after signup & tried multiple methods but user is getting redirected to either 2nd last blog or the same page (signup). Details below.
LINK TO SITE I NEED HELP WITH
GOAL
- A user reads 1 blog, clicks on 2nd blog & gets redirected to signup page.
- User gives his email & signs up.
- Signup page gets refreshed & shows back button (to go to last blog user was reading)
WHATS HAPPENING
For some reason, the "Last blog" from where user gets redirected to is not getting stored in
javascript: history.go(-1)
orwindow.history.back()
When I use
-1
in history functions mentioned above, it stays on the same sign page probably because it was refreshed but when I even user-2
in those above history functions, it goes to "2nd last blog" user read.
OTHER SOLUTION I TRIED
if (!sessionStorage.alreadyClicked) {
var referrer = document.referrer;
console.log(referrer);
localStorage.setItem("protectedReferrer", referrer);
var protectedReferrerForInsertion = localStorage.getItem("protectedReferrer");
console.log("Saved Variable was" protectedReferrerForInsertion);
}
document.querySelector("#back-button-referrer a").setAttribute("href", protectedReferrerForInsertion);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
Redirecting URLs don't get added to history or the referrer. You can't rely on those to record where to send somebody back to when that URL originally redirected.
They way to solve this issue is to use a URL parameter of the page to return to when redirecting to the signup page. In other words: /second-blog-article
should redirect to /signup?back=/second-blog-article
. That back
parameter needs to be passed the entire way through the sign up process and then you redirect to it once sign up is complete.
CodePudding user response:
Hey using session solved my problem. Here is the code.
jQuery(document).ready(function( $ ){
if (top.location.pathname === '/signup/'){
var sessionCheck = sessionStorage.getItem('visited');
if(sessionCheck == null || 0 ){
sessionStorage.setItem("visited", 0);
}
var visited = sessionStorage.getItem('visited');
if (visited == 0) {
var referrer = document.referrer;
sessionStorage.setItem("localReferrer", referrer);
sessionStorage.getItem('localReferrer');
$("#back-button-referrer a").attr("href", sessionStorage.getItem('localReferrer'));
sessionStorage.setItem("visited", 1);
}
else{
$("#back-button-referrer a").attr("href", sessionStorage.getItem('localReferrer'));
}
}
});
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>