Answer: Use the jQuery . attr() Method You can use the jQuery . attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.
click(function(e){ var id = e.target.id; alert(id); }); }); In this way, e. target is the element you have clicked on.
An anchor link (or "page jump") is a special URL that takes you to a specific place on a page. For example, the table of contents in this guide contains anchor links that take you to each heading.
you can use custom data attributes see this . <a href="#" data-json="{ 'myValue':'1'}">Click</a> //you can even pass multiple values there.
For current window, you can use this:
var hash = window.location.hash.substr(1);
To get the hash value of the main window, use this:
var hash = window.top.location.hash.substr(1);
If you have a string with an URL/hash, the easiest method is:
var url = 'https://www.stackoverflow.com/questions/123/abc#10076097';
var hash = url.split('#').pop();
If you're using jQuery, use this:
var hash = $(location).attr('hash');
You can use the .indexOf()
and .substring()
, like this:
var url = "www.aaa.com/task1/1.3.html#a_1";
var hash = url.substring(url.indexOf("#")+1);
You can give it a try here, if it may not have a #
in it, do an if(url.indexOf("#") != -1)
check like this:
var url = "www.aaa.com/task1/1.3.html#a_1", idx = url.indexOf("#");
var hash = idx != -1 ? url.substring(idx+1) : "";
If this is the current page URL, you can just use window.location.hash
to get it, and replace the #
if you wish.
Use
window.location.hash
to retrieve everything beyond and including the #
jQuery style:
$(location).attr('hash');
You can use the following "trick" to parse any valid URL. It takes advantage of the anchor element's special href-related property, hash
.
function getHashFromUrl(url){
return $("<a />").attr("href", url)[0].hash.replace(/^#/, "");
}
getHashFromUrl("www.example.com/task1/1.3.html#a_1"); // a_1
function getHashFromUrl(url){
var a = document.createElement("a");
a.href = url;
return a.hash.replace(/^#/, "");
};
getHashFromUrl("www.example.com/task1/1.3.html#a_1"); // a_1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With