Link to home
Start Free TrialLog in
Avatar of Giuregol
Giuregol

asked on

Anchor link without History entry

I have to make a link to an anchor tag (like <a name="hello">) in the same page that contains this tag, but I need that the a new history entry is created.
I tried with location.replace method but it doesn't seem to work if the target page is the same page that contains the link.
Any ideas?
PS: Sorry for the bad English!
Avatar of Zyloch
Zyloch
Flag of United States of America image

I'm not sure if this is possible. The location.replace only clears the browser history but doesn't add to it...

--Zyloch
Is there a particular reason you need the history to be recorded?

--Zyloch
Avatar of Giuregol
Giuregol

ASKER

As I said....my English i s VERY bad.

 I need that the a new history entry is NOT created !!!

I am not sure if that is completely possible... In a sense, location.replace replaces the current history with the new one. This means that it will create a new history, but the old one is deleted. I believe this is the only way to go. Is there a reason why the history needs to be suppressed? If the new history is an anchor, then it would be on the same page so it would not really matter.

--Zyloch
This is the reason:
I have a navigation menu (like in this site.. Home > All Topics > etc..) that not contains links but makes a simple history navigation. If the anchor links create new entries the navigation menu... simply fails!
I need that with an history.back() command I go to the previous page, not to the previous anchor.

I see... I won't ask why you are using history navigation, but I do know that this is a problem. Of course, going back more than one page isn't a problem, to go back two pages, just do:

<a href="javascript:history.go(-2)">Back</a>

You can have a cookie that will keep track of how many links are pressed, but that is tons more complex.

If possible, can you explain to me the needs to using history navigation? If the user hasn't visited the page, then, it won't work.

However, if you have a BACK button on your page and you wish it to work regardless of anchors, in your onLoad statement, you can have a simple check:

<body onload="var ref=document.referral; var locate=window.location; if (ref.substr(0,indexOf('.')) == locate.substr(0,indexOf('.'))) {window.location=history.back();}">

Of course, this would mean after an anchor is clicked and the user click the back button, the page will also jump. If you don't want that to happen, you can include extensions to the URL and get them with JavaScript.

--Zyloch
I have to use hisory navigation because this part of the site is made by others and I can't modify it.
I have to make pages that prevent menu to fail !
The navigation menu is on a separate frame  and is updated on the load of every page loaded in the main frame.
However the only way to solve the problem is making evry page have one history entry.
The functionality of location.replace method is good (if the old entry is deleted is OK for me) but simply it doesn't happen... the new entry is created but the old still there ! :-(
The location.replace method seems to fail if I add the '#name' on the URL.

Your solution doesn't work on my situation.
The onload event isn't fired when i click an anchor link on the same page.

I tried including extensions to the URL: this method works but the page reloads before scrolling and the users don't like that ( the documents are often too long).

ASKER CERTIFIED SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial