tags:

views:

105

answers:

4

For example I hide comments by default.

So if I find #comments I will be know that I need to show them up.

Is that possible to catch with php or javascript on page load?

+2  A: 

With JavaScript you can access the location.hash property.

window.onload = function(){
  alert(location.hash);
}
CMS
+1  A: 

PHP won't know about the #comments bit, so you'd have to use javascript to implement the functionality

David Caunt
A: 

In php you can access all parts of the URL using the parse_url() function http://php.net/parse-url

<?php
  $url = 'http://username:password@hostname/path?arg=value#anchor';

  $url = parse_url( $url );

  echo $url['fragment'];  // will output 'anchor'
?>

Edit: My apologies guys, although my example works and is fine for server-side generated urls, you are correct in the context of this question. Fragments are not a method I use extensively, although I do use parse_url quite often so it popped to mind immediately.

To give some value added content to this particular question, I would use jQuery for the task of revealing the comments that were hidden originally. It's easy to use and has some slick animations if you're looking for that sort of thing.

e.g.

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
    $(document).ready( function() {
     $("a.toggleComments").click( function() {
      $(".comment").toggle();
      return false;
     });
    });
</script>
<style>
    .comment {
     display: none;
    }
</style>
</head>
<body>
  <p>Some text that is shown by default. <a href="#" class="toggleComments">Toggle Comments</a></p>
  <p class="comment">A comment paragraph, originally hidden</p>
</body>
</html>
PHPexperts.ca
PHP can parse the fragment, but it's not sent in the request, so it won't be available in the $_SERVER['REQUEST_URI']
David Caunt
A: 

PHPExperts is, sadly, wrong..

The #fragmant part of any url is not actually sent to the server. The browser will strip out everything after the # when doing the request.

dcaunts suggestion is on the right track, you can really only use it within javascript.

Evert