4 Replies - 1061 Views - Last Post: 11 May 2010 - 03:57 AM

#1 blakpower  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 09-February 09

URL with anchor issue

Posted 30 April 2010 - 09:07 AM

Hi

I have a navigation menu with anchors in it, i.e. www.test.com/test.php#test. using the following code i can retreive the anchor value "#test" and display it on the page:
<script language="javascript" type="text/javascript">

var url = window.location.hash;
document.write(url);
</script>


the issue i am having is that. once i have clicked on a link with an anchor, the anchor is displayed but when i click on another link with an anchor the value is not updated. I am fairly new at javascript. i tried playing around with "window.location.reload()" but the page just kept reloading. Can anyone help.

any help will be appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: URL with anchor issue

#2 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: URL with anchor issue

Posted 30 April 2010 - 02:02 PM

When you click another link, a new page should load. So then the value wouldn't be "updated," but rather just recreated. This is if you're going from page A to page B: "pageA.html#anchor" -> "pageB.html#anchor".

But I think in your case you're link is taking you to another anchor on the same page, right? If this is the case, then your problem is that the script is not running again. Here's what you should do in that case:

Stick your code in a function:
function writeHash() {
// ... your existing code ...
}


And add an event handler to the link you click:
<a href="x.html#a" onclick="writeHash">Link Text</a>

Was This Post Helpful? 0
  • +
  • -

#3 blakpower  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 09-February 09

Re: URL with anchor issue

Posted 05 May 2010 - 03:11 AM

View Postforest51690, on 30 April 2010 - 01:02 PM, said:

When you click another link, a new page should load. So then the value wouldn't be "updated," but rather just recreated. This is if you're going from page A to page B: "pageA.html#anchor" -> "pageB.html#anchor".

But I think in your case you're link is taking you to another anchor on the same page, right? If this is the case, then your problem is that the script is not running again. Here's what you should do in that case:

Stick your code in a function:
function writeHash() {
// ... your existing code ...
}


And add an event handler to the link you click:
<a href="x.html#a" onclick="writeHash">Link Text</a>


Hi

I have added the function
function writeHash(){
url = window.location.hash;
document.write(url);
}


and i have added the onclick to the links
		    <ul>
		      <li><a href="#text" onclick="writeHash()">text</a></li>
  <li><a href="#test" onclick="writeHash()">test</a></li>
  <li><a href="#abc" onclick="writeHash()">abc</a></li>
		    </ul>


There is a slight issue. if i click on "text" first the value is not written but if i click on "abc" the value that is shown is "text" and if i click on "test" the value that is shown is "abc". its basically shows the previous vale rather than the current on.

can you help on this
Was This Post Helpful? 0
  • +
  • -

#4 jrm402  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 333
  • Joined: 18-March 10

Re: URL with anchor issue

Posted 05 May 2010 - 11:10 AM

This is because your Javascript is processed before the link is executed. So at first you will get nothing because no link has been clicked and that makes sense and so on you will get the selection before. Why don't you try passing the parameter of the hash to the JS function?

<li><a href="#text" onclick="writeHash('text')">text</a></li>
<li><a href="#test" onclick="writeHash('test')">test</a></li>
<li><a href="#abc" onclick="writeHash('abc')">abc</a></li>



Hope this helped :)
Was This Post Helpful? 0
  • +
  • -

#5 blakpower  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 09-February 09

Re: URL with anchor issue

Posted 11 May 2010 - 03:57 AM

View Postjrm402, on 05 May 2010 - 10:10 AM, said:

This is because your Javascript is processed before the link is executed. So at first you will get nothing because no link has been clicked and that makes sense and so on you will get the selection before. Why don't you try passing the parameter of the hash to the JS function?

<li><a href="#text" onclick="writeHash('text')">text</a></li>
<li><a href="#test" onclick="writeHash('test')">test</a></li>
<li><a href="#abc" onclick="writeHash('abc')">abc</a></li>



Hope this helped :)



Hi,

Thanks it did. i changed the function to make it easier
function writeHash(url){
document.write(url);
}


then i just added the value to each link
<li><a href="#text" onclick="writeHash('text')">text</a></li>
<li><a href="#test" onclick="writeHash('test')">test</a></li>
<li><a href="#abc" onclick="writeHash('abc')">abc</a></li>



I have one more issue, i am not sure if this is the right place. I want to pass the url value into a PHP variable i tried using SESSIONs but it didnt work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1