How to pass parameters after # (pound sign) in URL implementation

Source: Internet
Author: User

When I browse the Internet, I see some URLs of websites using the # number to pass some characters as parameters, because the content after # (well) in the URL is not added to the HTTP request, so I studied it and shared it with you.

Url:http://xxx.com/index.php?id=1#01234abcd

You can see that the string behind this # is used as an identity, so how does PHP (in PHP) get this value?

In fact, PHP can not directly get this value! But we can do it in a flexible way. It is commonly used by JS to set this value into a cookie, and then PHP will be able to access the cookie data:

Javascript code var SS = Window.location.href.split ("#");d ocument.cookies = "ss=" +ss[1];

You can then use $_cookie[' SS ' in PHP to get this value.

<script language= "JavaScript" > var ss =location.href.split ("#");    Document.cookie= ' ss= ' +ss[1];     if (ss[1]! = "<?php echo $_cookie[' SS '];?>") {//To determine if the COOKIE is not up-to-date and not reload the page window.location.reload (); }</script> <?php Echo $_cookie[' SS '];?>

One drawback of this approach is that it is necessary to reload the cookie after it has been set, the PHP side will take effect, and the user experience of slow speed is poor.


Another common scenario is to use JS to get the parameters, based on the value of the PHP from the end of the AJAX data display.

such as url:http://xxx.com/#news

Location.hash can get the # number in the URL and the back part var mod = location.hash;if (mod = = "#news") {//ajax gets and processes the latest data $.ajax (...);} else if (mod = = "#hots") {//ajax gets and processes hot data $.ajax (...);}

The second method is more suitable for common AJAX applications, and programming is flexible and changeable.


============================================

Just started to learn to write some blog, write a bad place please forgive me, feel that learned to pay attention to the next bar ~



How to pass parameters after # (pound sign) in URL implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.