假设URL为:
www.example.com/?val=1#part2
PHP可以使用GET数组读取请求变量val1。
哈希值part2也是可读的吗?或者这仅仅取决于浏览器和JavaScript?
假设URL为:
www.example.com/?val=1#part2
PHP可以使用GET数组读取请求变量val1。
哈希值part2也是可读的吗?或者这仅仅取决于浏览器和JavaScript?
当前回答
是什么:
动态抓取#散列
<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;
?>
为了使它更流利:
使用Javascript和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; ?>
其他回答
是什么:
动态抓取#散列
<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;
?>
为了使它更流利:
使用Javascript和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; ?>
它可以从Javascript中检索-作为window.location.hash。从那里,你可以用Ajax将其发送到服务器,或者将其编码并放入url中,然后可以传递到服务器端。
我们也可以用另一种方法来做,比如首先从js中获取哈希值,然后使用该参数调用ajax,可以做任何我们想做的事情
我认为哈希值只在客户端使用,所以在php中无法获得。
你可以用javascript重定向到PHP。
URI中#后面的部分被称为“片段”,根据定义,仅在客户端可用/处理(参见https://en.wikipedia.org/wiki/Fragment_identifier)。
在客户端,可以使用带有window.location.hash的javaScript进行访问。