0

I am trying to echo the hash at the end of the URL.

This didn't work

<?php 

echo '<script type="text/javascript">
$(this).hash.substring(1);</script>';?>

Any ideas?

3
  • 1
    This has nothing to do with PHP really, as you're outputting Javascript. Not important but for future tagging Commented Feb 20, 2011 at 18:02
  • Do you know what $(this) refers to in your code? Commented Feb 20, 2011 at 18:04
  • It is would be better if your make correction with the TAGS, wrong tags can make you question cannot be see. like me setting C++ to be hidden from question list. Commented Feb 20, 2011 at 18:12

2 Answers 2

1

Try location.hash.substring(1) instead:

echo '<script type="text/javascript">
var fragment = location.hash.substring(1);</script>';

Note that this PHP code does only echo HTML and JavaScript code to get that fragment value. PHP itself cannot retrieve that part of the URI as it is not part of the requested URI but only locally available.

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you're trying to combine two languages, but I don't think it's possible to do what you want this way. At a guess, you're trying to get the "#hashbit" from a url like this:

http://www.example.com/page.php?something#hashbit

Unfortunately, the browser doesn't send the "#hashbit" to the server. It's only used on the client side. You could write some JavaScript to grab the "#hashbit" to the server separately.

2 Comments

"You could write some JavaScript to grab the "#hashbit" to the server separately." That's exactly what he's doing; the problem would seem to be that the JavaScript isn't working, because of a confusion over the use of the implicit this variable.
It looked to me like what he was doing was trying to get the hash to PHP by executing the JS from within PHP. Or it was a JS question and the PHP wasn't relevant. Either way, confusion abounds :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.