Proper Url Forming with a Query String and an Anchor Hashtag

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



Leave a reply



Submit