How to redirect previous page after redirect to previous page.
After form submission i am redirect form to thank you page but after sometimes i want redirect previous page.
Below is the code i am using
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
//location = 'abc.com1/thank-you/';
if ( '17615' == event.detail.contactFormId ) {
}
else if( '19110' == event.detail.contactFormId ){
location = 'abc.com1/thank-you-broucher/';
}
else {
location = 'abc.com1/thank-you/';
}
}, false );
</script>
after location redirect i want again redirect to my first original page.
Anyone have idea then let me know
3
Answers
use history.back
You can do it several ways:
another way would be:
Or as mentioned above my post
and for last you can create a function
If you want to have it done automatically you just need to use it in a function and add a setTimeout:
Since your using WordPress, I would recommend doing this using the functions.php file and add a filter with a function for the redirect, but since you requested how to do this with javascript, you need to inspect your thank you page to grab the page-id-[ID-NUMBER] class it generates on the body tag your script above would need a condition to detect if the class is present on the body assuming this script is loaded on your entire site:
I prefer history.go(); since you can go back even more than 1 page back for example history.go(-2); would go back 2 pages instead of one, this sometimes can be handy in certain cases.
One of the problem with using
js
built inhistory.go(-1);
is that the behaviour is based on your browser and not on your website structure.If someone decide to access a website straight from a url or a bookmark, upon using
history.go(-1);
on landing, he would be redirected outside the website environment.As you’re using WordPress
We can easily build a go back url function based on the current user url.
Anything that can be offloaded to server should be. Instead of using
js
here is aphp
alternative. The main advantage here is quartering the user to your website. Here is ourget_backward_url()
function.Then, anytime we need to use it in our template we can simply output it on the front end…
Happy coding!