2011-05-25 110 views
6

試圖打印$ _ SERVER數組中的PHP,但我找不到我想要的東西:獲取完整的URL與#tag

http://www.domain.com/#sometaginpage

我希望「sometaginpage」。

幫助。謝謝!

+0

由於瀏覽器..我認爲是這樣的 – Harsh 2011-05-25 03:15:58

+3

'window.location.hash'在JavaScript中? – Marty 2011-05-25 03:58:09

+0

[如何獲得「somepage.php#name」中的哈希值後的值的可能重複?](http://stackoverflow.com/questions/1917762/how-to-get-the-value-after-the- hash-in-somepage-phpname) – PhoneixS 2015-07-08 09:38:06

回答

9

瀏覽器實際上並沒有發送散列(#)之後的任何內容,因爲它在瀏覽器中已解析。

+0

它不是通過JavaScript解析的。它通過一個錨標籤解決。 – netcoder 2011-05-25 03:36:50

+0

謝謝你指出,我錯了,並編輯了我的答案。 – 2011-05-25 03:52:15

+0

Thanks guys,I just ended ended Javascript: var url = window.location.toString(); if(url.indexOf(「STRING_HERE」)!= - 1){window.location =「.URL_HERE」;} – johnshaddad 2011-05-25 03:54:57

0

我不確定$ _SERVER ['REQUEST_URI']是否會放棄它。 s992可能是對的,它可能不會將它發送到服務器。

3

相當肯定,#hashtags不會被髮送到服務器,但你可以開發具有AJAX解決方法:

一些-page.html中

<script type="text/javascript"> 
    $(document).ready(function() { 
     $(window).bind('hashchange', function() { 
      var hash = window.location.hash.substring(1); 
      $.get('ajax-hash.php', { tag: hash }, 
       function(data) { $('#tag').html(data); } 
      ); 
     }); 
    }); 
</script> 

<div id="tag"></div> 
<a href="#one">#one</a> | <a href="#two">#two</a> | <a href="#lolwut">#lolwut</a> 

Ajax的hash.php

<?php 
    $hash = isset($_GET['tag']) ? $_GET['tag'] : 'none'; 
    echo $_SERVER['HTTP_REFERER'] . '#' . $hash; 
?> 

注:這取決於實際發送HTTP_REFERER的瀏覽器。由於它是通過jQuery完成的,它應該..但是沒有承諾! (防病毒/防火牆喜歡從您的數據包中刪除)

+0

好的一段代碼!對我來說工作得很好。 – leymannx 2015-05-13 10:02:22