Proper URL forming with a query string and an anchor hashtag
?var=var#hash
Everything after #
is client side.
Also, look into URL rewriting to get rid of ugly ?var=var
.
HTML anchor and query string
The query goes before the anchor, so:
http://example.com/page.php?parameter=value#anchor
Using a url query string to jump to page sections
Using your example, the link would be:
<a href="http://www.mydomein.com/page1#section2">Please read section 2</a>
Also note that the <a name="section2"></a>
is superfluous. You could simplify to
<h2 id="section2">Section 2</h2>
Get fragment (value after hash '#') from a URL
If you want to get the value after the hash mark or anchor as shown in a user's browser: This isn't possible with "standard" HTTP as this value is never sent to the server (hence it won't be available in $_SERVER["REQUEST_URI"]
or similar predefined variables). You would need some sort of JavaScript magic on the client side, e.g. to include this value as a POST parameter.
If it's only about parsing a known URL from whatever source, the answer by mck89 is perfectly fine though.
Using Hash (#) instead of Query string is there any advantage
The hashed url is often used in place of actually going back to the server for another page (that is, the page load is intercepted by Javascript).
The hash and anything after it is not usually sent to the server...
Imagine the following scenario...
<a id="SomeLink" href="#DoSomething">
You then attach an onclick using Javascript...
$("#SomeLink").click(function() {...})
If that click function doesn't return false
, the url will now have a #DoSomething
on the end of it, even though a request to the server hasn't been made.
It can also be useful as a placeholder for manipulating the history so you can have greater control over the Back/Forward buttons (or many otherclient-side javscript tricks). See history.js as an example.
Related Topics
Generating a Single Entity from Existing Database Using Symfony2 and Doctrine
How to Create a Custom Admin Page in Opencart
What Do the Ent_Html5, Ent_Html401, ... Modifiers on HTML_Entity_Decode Do
How to Convert Ipv6 from Binary for Storage in MySQL
Process CSV into Array with Column Headings for Key
In PHP, What Is the Differences Between Null and Setting a String to Equal 2 Single Quotes
MySQL Error "Too Many Connections"
How to Re-Format Datetime String in PHP
What Is the Fastest Xml Parser in PHP
Executing PHP Code Inside a .Js File
Running Composer Returns: "Could Not Open Input File: Composer.Phar"
PHP - How to Build Tree Structure List
Switching Between Http and Https Pages with Secure Session-Cookie
Excel Date Conversion Using PHP Excel
Accessing Variables and Methods Outside of Class Definitions
How to Set a Default Attribute Value for a Laravel/Eloquent Model