What is Fragment URLs and why to use it

你说的曾经没有我的故事 提交于 2019-12-23 07:49:21

问题


I am new in PHP Development.

Today I came across the interesting topic URL fragmentation specifically the '#' part of the URLs.

I searched about that It says it's like www.example.com\foo.html#bar.

But I don't understand why this "#bar" is needed. and how to read it by PHP?


回答1:


A fragment is an internal page reference, sometimes called a named anchor. It usually appears at the end of a URL and begins with a hash (#) character followed by an identifier. It refers to a section within a web page.

In HTML documents, the browser looks for an anchor tag with a name attribute matching the fragment.

There are a few things about the fragments, the most important may be that they aren't sent in HTTP request messages but you can find some more info about them on this page.

Javascript can manipulate fragments on the current page which can be used to to add history entries for a page without forcing a complete reload.




回答2:


It's unable to read it by php. It uses by client side (browser) for hash navigation, but you can write JS code to handle hash change and send async request to your server side (php) and display result on your page.




回答3:


For reading a fragment by the PHP you can use 'parse_url( $url, PHP_URL_FRAGMENT )' function. This function in a built-in PHP function. follow example can help you to understand how to use it:

$url = 'www.example.com\foo.html#bar';
echo '<pre>';
var_dump(parse_url($url, PHP_URL_FRAGMENT));
echo '</pre>';

the result is:

string(3) "bar"

for more information about parse_url you can read this page



来源:https://stackoverflow.com/questions/30997420/what-is-fragment-urls-and-why-to-use-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!