Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.location.hash returns hash tag in front of value

I have the following code in MVC3 view:

 $(document).ready(function () {
    if (window.location.hash) {
        var manager= new Manager();

        manager.doSomeStuff(window.location.hash);
    }
});

The interesting thing is that when there is no hash tag in the URL, or there is only a hash tag example:

http://localhost:1223/Index/AboutUs

http://localhost:1223/Index/AboutUs#

When the window.location.hash is empty and the function is not executed. But when there is some value in the hash tag:

http://localhost:1223/Index/AboutUs#categoryId=5&manufacturerId=8

The value in the window.location.hash is #categoryId=5&manufacturerId=8

Can you explain to me why the # tag is included in the value and why when there is no value after the # tag, the window.location.hash is empty.

like image 613
Ivan Stoyanov Avatar asked Oct 30 '12 16:10

Ivan Stoyanov


People also ask

What does Window location hash return?

The hash property of the Location interface returns a string containing a '#' followed by the fragment identifier of the URL — the ID on the page that the URL is trying to target.

What does location hash do?

The Location Hash property in HTML is used to return the anchor part of a URL. It can also be used to set the anchor part of the URL. It returns the string which represents the anchor part of a URL including the hash '#' sign.

Which location property will give you current hash value in the URL?

The location. hash property sets or returns the anchor part of a URL, including the hash sign (#).

What is a window hash?

Overview. The window. location. hash returns a string that contains a # along with the fragment identifier of the URL. The fragment identifier of the URL starts with a # followed by an identifier that uniquely identifies a section in an HTML document.


4 Answers

There's nothing much to explain. It is the way it works.

Read more here: http://www.w3schools.com/jsref/prop_loc_hash.asp

Definition and Usage

The hash property returns the anchor portion of a URL, including the hash sign (#).
like image 135
Pulkit Mittal Avatar answered Oct 18 '22 06:10

Pulkit Mittal


You can change it if you want by simply changing the hash name:

//Your old hash name caught in a variable
var nameHash = location.hash;

//Your new hash name without "#"
var newHashName = nameHash.replace("#","");
like image 8
Erick Jones Avatar answered Oct 18 '22 05:10

Erick Jones


var hash = window.location.hash.substring(1);

This omits the first character of the string, which is the hash tag.

like image 8
Not Fake Avatar answered Oct 18 '22 05:10

Not Fake


You can repalce # but this way will create conflict and won't work with javascript.

Here is window.location reference link.

Here is different usage examples:

$(document).ready(function () {
    var urlHash = window.location.hash;
    var sampleURL = '#categoryId=5&manufacturerId=8';

    if ( urlHash.length > 1 ) {
       //do stuff
    }else{
       //if value is empty, do stuff
    }

    if ( urlHash === sampleURL ) {
       commonResponse();
    }

    $('a').click(function() {
       var target = $(this).attr('href');

       if (target === sampleURL ) {
          commonResponse();
       }    
    });

    function commonResponse() {
       //alert('ok');
    }
});
like image 1
Barlas Apaydin Avatar answered Oct 18 '22 07:10

Barlas Apaydin