Edit: What I really need to know is if there is any javascript event that will reliably fire when the user arrives at the page via the back button. I trie
I don't think you can test for the back button specifically, but I believe you can check the browser's history position with Javascript.
The answer by @tkotitan was probably correct when he wrote it. His answer led me to my solution. It's not foolproof, but it might be "good enough" to help in some situations.
I improved my script and replaced it with the following. This version is for an intermediate page that now works as intermediate in both directions.
<?php
if($_SERVER['QUERY_STRING']=='')
{
echo '<a href="go_back_test.php?page=1">Go forward</a>';
}
elseif($_GET['page']!=2)
{
echo 'You cannot stay here.';
echo '
<script type="text/javascript">
function test()
{
if (document.getElementById("detectback").value=="goneback")
{
document.getElementById("detectback").value=history.length;
}
if (document.getElementById("detectback").value==history.length)
{
document.getElementById("detectback").value="goneforward";
window.location = "go_back_test.php?page=2"
}
else
{
document.getElementById("detectback").value="goneback";
window.history.back();
}
}
function updateform()
{
if(document.getElementById("detectback").value=="")
{
document.getElementById("detectback").value=history.length;
}
}
</script>
<img src="spacer.gif" onload="updateform(); test();"><br>
<form>
<input id="detectback" type="text" value="" style="display: none;">
</form>
';
}
else
{
echo 'Welcome to page 2';
}
?>
Yes. In javascript you can use visited.value
function checkrefresh()
var visited = getCookie("visited");
{
if((form.visited.value == '' || form.visited.value == null) && (visited != '1') )
{
document.form.visited.value = "1";
document.cookie = "visited=1";
//here you set it to something so that next time they go to the page it will no longer be nothing and you can 'disable the button' see below.
}
else
{
document.form.submitbutton.disabled=true;
}
}
function getCookie(name)
{
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}
you would call this in the onload tag FYI.
Isn't that the behaviour you want, so they don't double-submit using the back button?
Anyway, you could set a cookie on the following page, and detect that on load on the page with the form.