Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple hash like parameters in url

I am building a dynamic website using jQuery and Ajax. I have solved the problem of history and back button using the History Plugin. I want to pass multiple hash like parameters in url without reloading the whole page on click events. I want something like:

  • something.php#type=abc&id=123 or
  • something.php/?type=abc&id=123...

Which could be the best way to pass and retrieve such kinda paramters in url?

<a href="#type=abc&id=123">Click</a>
if(type==abc && id==123)
{
    do this..
}

Normally we do this, <a href="something.php?type=abc&id=123">Click</a> but it reloads the whole page.

like image 514
roy Avatar asked Aug 13 '11 07:08

roy


People also ask

Can a URL have multiple fragments?

A URL cannot have more than one fragment. URL parameters are passed in key-value pairs. URL fragments comprise just a string of text after the hash (#). Changing the URL parameters or their values and hitting enter reloads the page.

What is hash parameter in URL?

A hash sign (#) in a URL is referred to as a fragment. Historically, URL fragments have been used to automatically set the browser's scroll position to a predefined location in the web page. In that sense, if a URL refers to a document, then the fragment refers to a specific subsection of that document.

Can we hash in URL?

In a URL, a hash mark, number sign, or pound sign ( # ) points a browser to a specific spot in a page or website. It is used to separate the URI of an object from a fragment identifier. When you use a URL with a # , it doesn't always go to the correct part of the page or website.

How do I add a hash to a URL?

The hash of a url can be found by creating a new URL Javascript object from the URL string, and then using its hash property to get the value of the hash fragment. Note that this will include the # character also. If the url does not contains a hash, then an empty string "" will be returned.


2 Answers

The hash value has a limited set of characters that are technically allowed to be in it. If you look at the URL spec here, the hash value is called a fragmentid. Here's the grammar for what it can contain:

fragmentid              xalphas
xalphas                 xalpha [ xalphas ] 
xalpha                  alpha | digit | safe | extra | escape
safe                    $ | - | _ | @ | . | &  | + | -
extra                   ! | * |  " |  ' | ( | )  | ,
escape                  % hex hex
alpha                   a | b | c | d | e | f | g | h | i | j | k |
                        l | m | n | o | p | q | r | s | t | u | v |
                        w | x | y | z | A | B | C | D | E | F | G |
                        H | I | J | K | L | M | N | O | P | Q | R |
                        S | T | U | V | W | X | Y | Z   
digit                   0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

You will notice that you can have &, but not = unless you call encodeURIComponent and decodeURIComponent on the hash value when setting it or retrieving it.

As for what technique to use, it's really up to you. You can make your own little syntax in the hash value if you need to represent multiple parameters there. I personally would probably separate the values with & just like in the query string, but use - instead of =.

like image 95
jfriend00 Avatar answered Nov 10 '22 23:11

jfriend00


In terms of URIs, you actually can do what you're asking.

@jfriend00 is correct in that you can't use the "=" in the name/value of a given hash value, however, I think they've misread the spec as to what that actually means. You also can't use the "=" character in the name/value in the query string of a URL. Why? Because it has the special meaning in denoting a key/value pair. It serves the same purpose in the hash (aka fragmentid), which is exactly how you're using it in your example.

If you look at the w3.org page for media fragments, the third sentence states:

The syntax is based on the specification of particular name-value pairs that can be used in URI fragment and URI query requests to restrict a media resource to a certain fragment.

Notice it says you can use key/value pairs for both the query and the hash part of a URI. If you look further down in the page, you'll find that it describes the format exactly as you're using it in the fragmentid:

A list of name-value pairs is encoded in the query or fragment component of a URI. The name and value components are separated by an equal sign (=), while multiple name-value pairs are separated by an ampersand (&).

That being said, JavaScript does not have a built-in way to access the key/value pairs in a structured way (just like it doesn't have a built-in way to access the key/value pairs of the query string). So, you'd have to build your own function to do that, and there are a couple other posts on StackOverflow that already show how to do that:

  • Parse URL # fragment with javascript
  • Parsing URL hash/fragment identifier with JavaScript
  • Split and parse window.location.hash
like image 41
jangosteve Avatar answered Nov 10 '22 23:11

jangosteve