I have this url: example.com/index.html#tab-1
I write into browser following url example.com/index.html#tab-1 and press enter - page will not load.
I want to get the parameter tab1 and load content with id tab1.
My code:
I call this functions:
//to load
showTabContent(gethash);
showTabContent: function(id)
{
if(id != null)
{
var tabid = id.split("-");
$("#tab"+tabid[1]).show();
}
},
gethash: function()
{
var hash = window.location.hash;
if($.trim(hash) != "") return hash;
else return null;
}
You can use this function to parse the URL into an object: http://james.padolsey.com/javascript/parsing-urls-with-the-dom/. After you get the object back, you can use the properties of the object to get the parts of the URL you want.
A similar answer was provided in this post: String URL to json object
Check this jQuery plugin:
JS File: https://raw.github.com/cowboy/jquery-hashchange/master/jquery.ba-hashchange.min.js
The solution for your code:
HTML:
JS: