Results 1 to 3 of 3

Thread: javascript retrieve hash url -> php

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default javascript retrieve hash url -> php

    i had a post in PHP section but i think my question is 1/2 JS. i have href links using hash anchors like
    Code:
    href="#/work/section1"
    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

    Code:
    <?php
    $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
    http://stackoverflow.com/questions/9...ion-of-the-url

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

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    HTML Code:
    <a href="#huh">Huh</a>
    If you click it and you have a script on the page (I know you're using jQuery):

    Code:
    jQuery(function($){
    	alert(location.hash);
    });
    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:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <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">
    jQuery(function($){
    	$('#sender').click(function(){
    		var hash = this.hash.substring(1);
    		if(hash){
    			$.ajax({url: 'data.php', data: {'anchor' : hash}, type: 'POST', cache: false, success: function(d){
    				$('#result').html(d);
    				}
    			});
    		}
    	});
    });
    </script>
    </head>
    <body>
    <div id="result">Result will go here</div>
    <a id="sender" href="#huh">Huh</a>
    </body>
    </html>
    data.php:

    Code:
    <?php
    	$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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    cool! thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •