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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing BACK button nature

Status
Not open for further replies.

Rush507

Programmer
Apr 30, 2008
1
Hello
I'm in this project where my boss wants me to change the behaviour of the back button so that instead of jumping to the previous url it un-does the effect.

I'll explain better

1. you click on a top menu and then you slowly scroll down to a certain part of the webpage. then you hit the browser's back button and you scroll up to the top.

2. you click on a picture and a lightbox comes up showing you the picture. then you click on the browser's back button and fade out the picture and keep in the page.

I've stated them that I just cannot change it's nature of going back to the previous item in the history.

But I want some comments about you to see if I'm right or wrong. (If wrong, gimme a hint so I can do it)
 
You're right. Tell them kaht said so.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
I think trying to change the behavior of something everyone is familiar with is a very bad idea. But if you really need to do this, you could attempt something like this: (I was really bored)
[ul][li]Create an array to track the user actions you want to be able to undo. [/li]
[li]After every action, change the location of a hidden Iframe to add an entry in the browser's history.[/li]
[li]In the Iframe's page load event, run a function that will either undo or redo the actions.[/li]
[/ul]

Here's what I had so far. [red]Warning![/red] The following code is complete garbage and doesn't work quite right. I'm just posting it here to steer you in the direction you want to go.

This is the content of the hidden iframe's pages. I needed two pages (a.html and b.html) and switch back and forth between them otherwise IE wasn't adding an entry in the history.
Code:
<body onload="parent.undoAction()"></body>

And here's the main page.
Code:
<script>
var ifrmPage;
var actions = [];
var lastActionIndex = 0;

function scrollPage(){
  alert("Page Scrolled");
}

function scrollPageUndo(){
  alert("Undo Page Scrolled");
}

function showImage(){
  alert("Image Shown");
}

function showImageUndo(){
  alert("Undo Image Shown");
}

function doAction(action){
  ifrmPage = ifrmPage == "a" ? "b" : "a";
  actions[actions.length] = action;
  window[action]();
  window.frames.ifrm.location.href = ifrmPage + ".html#" + (actions.length - 1);
  lastActionIndex = actions.length - 1;
}

function undoAction(){
  var hash = window.frames.ifrm.location.hash;
  if(hash.length > 0){
    var actionIndex = hash.substring(1);
    if(actionIndex < lastActionIndex){
      var undoAction = actions[lastActionIndex] + "Undo";
      window[undoAction]();
      lastActionIndex = actionIndex;
    }
  }
}

</script>
<body>
<a href="javascript:doAction('scrollPage')">Scroll Page</a>
<a href="javascript:doAction('showImage')">Show Image</a>

<iframe name="ifrm" src="b.html"></iframe>
</body>

Again, I don't think you should do this. I just worked on it a little to see if it was possible. Use this junk code at your own risk.

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top