View Full Version : Resolved javascript retrieve hash url -> php

09-22-2010, 05:12 PM
i had a post in PHP section but i think my question is 1/2 JS. i have href links using hash anchors like


this calls a "switch.php" file which is dynamic and i want to be able to read that ending part so i can write some conditions like

$id = $_GET['id'];
if($id=1 ){$link = "http://a.com/home"; $title="client1"; }
else if($id=2 ){$link = "http://b.com/home"; $title="client2"; }
else if($id=3 ){$link = "http://c.com/home"; $title="client3"; ;}

// the rest of html mark up

i saw this post but i dont really understand what they are suggesting

how do i get that has from client and send it to server so i can write my php conditions?

09-24-2010, 01:54 AM
From the link in your post:

The hash is never sent to the server, so no.

In fact, even though it is always sent to the browser, it doesn't always trigger an onload event for the page. For instance, if you have a link on your page:

<a href="#huh">Huh</a>

If you click it and you have a script on the page (I know you're using jQuery):


which will execute on DOM load. It will do nothing because the DOM is already loaded and clicking on the example link doesn't cause another DOM load.

FYI - the DOM load executes as part of every onload event, simply a little sooner.

Now, you can do something like this:

<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
var hash = this.hash.substring(1);
$.ajax({url: 'data.php', data: {'anchor' : hash}, type: 'POST', cache: false, success: function(d){
<div id="result">Result will go here</div>
<a id="sender" href="#huh">Huh</a>


$anchor = isset($_POST['anchor'])? $_POST['anchor'] : '';
echo $anchor;

and it will work because it's set on the link's click event and therefore doesn't rely upon a page load to detect a change in the hash.

09-25-2010, 09:55 PM
cool! thanks