Solved

prevent page anchor tag from creating a history item

Posted on 2004-04-21
5
1,487 Views
Last Modified: 2010-10-28
I've got a javascript back  (javascript:history.go(-1);) in the href of the page.  The page also has anchor tags href="#1", href="#2", ..., etc.  I'm looking for a way to prevent the anchor tags from creating a history item or to remove the specified history item after the item is clicked.  That way the javascript back button will actually go back to the page that referred this page and not click through the history.

Thank you.

Ch@rlie
0
Comment
Question by:crazycharlie
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:devic
ID: 10883965
try this:
===============
<script>
function goTo(where)
{
      document.location.replace(where);
      return false;
}
</script>
<a href=#aaa onclick="return goTo('#aaa')">AAA</a>
<a href=#bbb onclick="return goTo('#bbb')">BBB</a>
0
 

Author Comment

by:crazycharlie
ID: 10884025
devic,

Thanks for the reply.  This doesn't seem to clear the history value at all.  When I click the javascript back (or the browser back for that matter), it still goes from http://www.myurl.com/page.asp#1 to http://www.myurl.com/page.asp.  What I'm wanting it to do is prevent the anchor #1 from creating a listing in the history so the back button will go to the referred page.

Ch@rlie
0
 
LVL 25

Accepted Solution

by:
devic earned 125 total points
ID: 10884344
ok, here another version:
============================
<html>
<head>
<script>
function myAnchor()
{
      var s=event.srcElement;
      if(s.tagName.match(/a/i) && !s.href.indexOf(document.location+'#'))
      {
            document.getElementById((s.href.substr(s.href.indexOf('#')+1))).scrollIntoView(true);
            return false;
      }
      return true;
}
</script>
</head>

<body onclick="return myAnchor()">
<a id=top></a>
<a href=#aaa>AAA</a><br>
<a href=#bbb>BBB</a><br>
<br><img src=1.gif width=1 height=1000><br>
<a id=aaa>aaa</a><br>
<a href=#top>top</a><br>
<br><img src=1.gif width=1 height=1000><br>
<a id=bbb>bbb</a><br>
<a href=#top>top</a><br>

</body>
</html>
0
 

Author Comment

by:crazycharlie
ID: 10884494
devic,

This works perfectly!!! Thanks!

Ch@rlie
0
 

Expert Comment

by:Dorian_Fabre
ID: 34012670
Hi,

Your code (the 2nd one) works great on text links but I'm unable to get it to work on image buttons. My pages have a mix of text links to find information further down the same page and image buttons to take you back to the top again - is there any way the code can be applied to both?

Thanks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question