...

View Full Version : targeting named anchor and the back button.



persian-rice
10-30-2008, 07:06 PM
Hey everyone, my first post here, and I need some help.

I have 3 links that target to named anchors placed randomly on my single page.

When I click on "myurl.com#target1" for for example, it takes me to the anchor named "target1", which is great, I continue browsing the page and eventually click on a link that takes me to another page.

Now the problem occurs when I click the browser's back button, it takes me back to the "target1" anchor instead of the last position I was on the page.

How can I make sure that it takes me back to the last position of the page instead of going back to the last anchor I targeted to.

This site is viewed through a CD btw.

Thanks for any help.

itsallkizza
10-30-2008, 07:31 PM
What browser are you using? Because I just tested this:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
</head>
<body>

<a href="#one">one</a> <a href="#two">two</a> <a href="#three">three</a> <a href="http://www.google.com">outside link</a>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<div id="one">section one</div>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<div id="two">section two</div>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<div id="three">section three</div>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

</body>
</html>

and in both IE7 and FF, it doesn't follow the behavior you mentioned. It does in fact go back to your previous position and not the id.

persian-rice
10-30-2008, 07:46 PM
Thanks for the response.

Ok, I fooled around with what you tested with and It looks like it works fine for me as well, but I was able to reproduce my problem with a change, and this is my problem, I am opening a pdf document with my link. Try linking the external link to a pdf document in the same folder as the html and you will see the effect......I think.

Thanks for the help so far, at least I am one step closer.

itsallkizza
10-30-2008, 08:14 PM
Feels a tad hackish, but it works:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
<script type="text/javascript">
<!--
function onPageLoad()
{
window.location = "#top_of_page";
}

window.onload = onPageLoad;
// -->
</script>
</head>
<body id="top_of_page">

<a href="#one">one</a> <a href="#two">two</a> <a href="#three">three</a> <a href="test.pdf">outside link</a>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<div id="one">section one</div>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<div id="two">section two</div>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<div id="three">section three</div>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

</body>
</html>

persian-rice
10-30-2008, 08:27 PM
although that does work, I need to it to go to last position....is there a way to further develop a hacked script to do that?

itsallkizza
10-30-2008, 11:41 PM
Ah yes, sorry I had forgotten that... Still thinking. I tried a combo php/javascript redirect, which works, but it takes a quick double back button click, which is horriby user-unfriendly.

I'm leaving for a 2 hour drive home from work in 20 min, I'll brainstorm in the car.

EDIT: You could use cookies or session variables to store the user's last position and then send them there on the page load. I feel like there must be a simpler way, but if that's not over the top hackish for you I could set up the script when I get home.

persian-rice
10-31-2008, 03:35 PM
That sounds like an idea.

I'm a newb, but I made this with some research, it isn't working yet. I'm sure I am making a mistake somewhere in here.

<script type="text/javascript">
<!--
var scrollposition = document.body.scrollTop;
function onMouseDown()
{
SetCookie("ScrollPos", scrollposition);
}
function onPageLoad()
{
document.body.scrollTop = ReadCookie("ScrollPos");
}
// -->
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum