Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?

前端 未结 12 2390
无人及你
无人及你 2020-11-21 04:37

Assuming a URL of:

www.example.com/?val=1#part2

PHP can read the request variables val1 using the GET array.

Is the ha

相关标签:
12条回答
  • 2020-11-21 05:03

    Simple test, accessing http://localhost:8000/hello?foo=bar#this-is-not-sent-to-server

    python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"
    Serving HTTP on 0.0.0.0 port 8000 ...
    localhost - - [02/Jun/2009 12:48:47] code 404, message File not found
    localhost - - [02/Jun/2009 12:48:47] "GET /hello?foo=bar HTTP/1.1" 404 -
    

    The server receives the request without the #appendage - anything after the hash tag is simply an anchor lookup on the client.

    You can find the anchor name used within the URL via javascript using, as an example:

    <script>alert(window.location.hash);</script>
    

    The parse_url() function in PHP can work if you already have the needed URL string including the fragment (http://codepad.org/BDqjtXix):

    <?
    echo parse_url("http://foo?bar#fizzbuzz",PHP_URL_FRAGMENT);
    ?>
    
    Output: fizzbuzz
    

    But I don't think PHP receives the fragment information because it's client-only.

    0 讨论(0)
  • 2020-11-21 05:03

    What about:

    Dynamically grab the #hash

    <script>
    var urlhash = window.location.hash, //get the hash from url
        txthash = urlhash.replace("#", ""); //remove the #
        //alert(txthash);
    </script>
    
    <?php
    $hash = "<script>document.writeln(txthash);</script>";
    echo $hash;
    ?>
    

    To make it more fluent:

    Full Example using just Javascript and PHP

    <script>
    var urlhash = window.location.hash,  //get the hash from url
    txthash = urlhash.replace("#", "");  //remove the #
    
    function changehash(a,b){
       window.location.hash = b; //add hash to url
       //alert(b); //alert to test
       location.reload(); //reload page to show the current hash
    }
    </script>
    
    <?php $hash = "<script>document.writeln(txthash);</script>";?>
    
    <a onclick="changehash(this,'#hash1')" style="text-decoration: underline;cursor: pointer;" >Change to #hash1</a><br/>
    <a onclick="changehash(this,'#hash2')" style="text-decoration: underline;cursor: pointer;">Change to #hash2</a><br/> 
    
    <?php echo "This is the current hash: " . $hash; ?> 
    
    0 讨论(0)
  • 2020-11-21 05:06

    The answer is no.

    The main purpose of the hash is to scroll to a certain part of the page where you have defined a bookmark. e.g. Scroll to this Part when page loads.

    The browse will scroll such that this line is the first visible content in the page, depending on how much content follows below the line.

    Yes javascript can acces it, and then a simple ajax call will do the magic

    0 讨论(0)
  • 2020-11-21 05:06

    We can do it with another approach too, Like first of all get the hash value from js and call the ajax using that parameter and can do whatever we want

    0 讨论(0)
  • 2020-11-21 05:08

    Yes you can:

    Use this method to prevent errors:

    <script> 
    query=location.hash;
    document.cookie= 'anchor'+query;
    </script>
    

    And of course in PHP, explode that puppy and get one of the values

    $split = explode('/', $_COOKIE['anchor']);
    print_r($split[1]); //to test it, use print_r. this line will print the value after the anchortag
    
    0 讨论(0)
  • 2020-11-21 05:09

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

    0 讨论(0)
提交回复
热议问题