0

My Page name is index.php and its contain some links(internal page link) and contents. on the click of link I m applying "selected" class on link li and showing related contents and my page link change from index.php to index.php#c2 (or #c3, #c4, c1 for other ids).

My problem is on the page load. If I give this link in other page eg. in page.php I given like this <li><a href="index.php?#c2">link2</a></li> then how could I know that #c2 is passed in URL based on this I want to apply the "selected" class to li. I tried it by $_SERVER but not done. I am not able to get the string after "?".

Pls tell me if there is any other way to do this..

<li class="selected"><a href="#c1">link1</a></li>
<li><a href="#c2">link2</a></li>
<li><a href="#c3">link3</a></li>
<li><a href="#c4">link4</a></li>

<div id="c1"><!-- contents of link1 --></div>
<div id="c2"><!-- contents of link2 --></div>
<div id="c3"><!-- contents of link3 --></div>
<div id="c4"><!-- contents of link4 --></div>

Jquery code to add selected class

$('.links a').click(function(){
    $('.links  a').parent().removeClass('selected');
    $(this).parent().addClass('selected');  
});

Thanks in advance..

Shahid Ahmed
  • 494
  • 2
  • 4
  • 10
  • Duplicates! http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript - http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery?lq=1 – Undefined Jul 19 '12 at 11:02
  • Please refere this answer http://stackoverflow.com/questions/298503/how-can-you-check-for-a-hash-in-a-url-using-javascript – Rajan Rawal Jul 19 '12 at 11:05

3 Answers3

3

You can't do this in PHP, only because it's a client mechanic and PHP is on the server :) You have to parse your query string directly in JS.

Try something like this :

$('a [href='+document.location.hash+']').parent().addClass('selected');
PoulsQ
  • 1,936
  • 1
  • 15
  • 22
1

I think this would work... e.g for

example.com/page.html#anchor
example.com/page.html#anotheranchor

sol'n is...

if(window.location.hash) {
  // Fragment exists
} else {
  // Fragment doesn't exist
}
Rajan Rawal
  • 6,171
  • 6
  • 40
  • 62
0

you can use regexp in either server script or javaScript, than defining clss for each of the element. simply use regexp pattern given below and it will give you only link part from the URL.

     ^.*index.php/?(.*)$ 

To see how to use RegExp Object in JavaScript follow the link.

Diablo Geto
  • 457
  • 4
  • 21