Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

run javascript on back button

Status
Not open for further replies.

tyutghf

Technical User
Apr 12, 2008
258
GB
I have a little script that enables a link on a button if a checkbox is ticked. Basically the user has to agree that the link is an external one by ticking a box, then the link becomes active.

When people browse away from the page then press the back button the checkbox remains ticked but the links are inactive, the user has to untick the box then retick the box. Is it possible to make the link active when using the back button?

Here is what I am doing

Code:
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
var [ignore]link[/ignore]1 = $("#to-toggle");
var [ignore]link[/ignore]2 = $("#to-toggle2");
$("#toggle").on("change", function() {
    if(this.checked) {
        [ignore]link[/ignore]1.attr("href", [ignore]link[/ignore]1.data("href"));
	[ignore]link[/ignore]2.attr("href", [ignore]link[/ignore]2.data("href"));
    } else {
        [ignore]link[/ignore]1.removeAttr("href");
	[ignore]link[/ignore]2.removeAttr("href");
    }
});

});//]]>  

</script>

<input id="toggle" type="checkbox">

<a id="to-toggle" data-href="link"><img src="image.png" /></a>
<a id="to-toggle2" data-href="[ignore]link[/ignore]2"><img src="image2.png" /></a>
 
Browsers simply load the already rendered page from the local cache, so script event are not triggered

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
You could try using 'setTimeOut' which runs a function to analyse the checkboxes and unlock as necessary.

I did a test page to check setTimeOut continued to run when you navigate away from the page and then use the back button.


wait for 5 seconds and you will get a popup alert, use the link to navigate away, click the browser back button and wait 5 seconds.

You should see the popup again.


"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top