Rob*_*cks 212 language-agnostic url http uri-fragment web
假设URL为:
www.example.com/?val=1#part2
Run Code Online (Sandbox Code Playgroud)
PHP可以val1使用GET数组读取请求变量.
哈希值是否part2也可读?或者这只是浏览器和JavaScript?
Ion*_*tan 199
主要问题是浏览器甚至不会发送带有片段部分的请求.片段部分在浏览器中解析.所以它可以通过JavaScript访问.
无论如何,您可以使用parse_url()将URL解析为位,包括片段部分,但显然不是您的情况.
tom*_*tom 98
简单测试,访问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 -
Run Code Online (Sandbox Code Playgroud)
服务器在没有#appendage的情况下接收请求 - 在哈希标记之后的任何内容都只是客户端上的锚点查找.
您可以通过javascript使用URL中找到锚名称,例如:
<script>alert(window.location.hash);</script>
Run Code Online (Sandbox Code Playgroud)
如果您已经拥有包含片段(http://codepad.org/BDqjtXix)所需的URL字符串,PHP中的parse_url()函数可以工作:
<?
echo parse_url("http://foo?bar#fizzbuzz",PHP_URL_FRAGMENT);
?>
Output: fizzbuzz
Run Code Online (Sandbox Code Playgroud)
但我不认为PHP接收片段信息,因为它只是客户端.
Ali*_*man 48
它可以从Javascript中检索 - as window.location.hash.从那里你可以用Ajax将它发送到服务器,或者编码并将其放入URL然后可以传递到服务器端.
小智 8
是的,这是真的,服务器没有获得锚点部分.但是有一种使用cookie的解决方法.你可以在这里找到它:http://www.stoimen.com/blog/2009/04/15/read-the-anchor-part-of-the-url-with-php/
答案是不.
哈希的主要目的是滚动到已定义书签的页面的某个部分.例如,页面加载时滚动到此部分.
浏览将滚动,使得此行是页面中的第一个可见内容,具体取决于该行下方有多少内容.
是的,javascript可以访问它,然后一个简单的ajax调用将做的魔术
关于什么:
动态获取#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;
?>
Run Code Online (Sandbox Code Playgroud)
为了让它更流畅:
仅使用 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; ?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
210412 次 |
| 最近记录: |