How to write a javascript condition if someone clicks on a url that contains hashtag at the end

javascript get current url
javascript get url path
javascript redirect to relative url
javascript get url parameter
javascript redirect to url
javascript get root url
javascript get base url
javascript get domain from url

How to fire a javascript condition on a webpage if a visitor clicks on a url that contains hashtag at the end. For example like if I click on an anchor tag i.e. abc.com/page.html# then condition runs and return true.

I tried to do the following

if (document.querySelector("a[hash='"+hashValue+"']").click() ) 
// do something;

but its not working. Any clue?

Add an event listener to the window (document, body, or any other element that contains all the links will also work), check if the element is an anchor and use String#endsWith on its href.

addEventListener("click", function(event) {
  if (event.target instanceof HTMLAnchorElement && event.target.href.endsWith("#")) {
    console.log("Hash");
  }
});
<a href="about:blank" target="_blank">Test without #</a>
<a href="about:blank#test" target="_blank">Test with #</a>
<a href="about:blank#" target="_blank">Test with hash at end#</a>
<button>Invalid Button 1</button>
<button href="about:blank#">Invalid Button 2</button>

Get URL and URL Parts in JavaScript, is the URL() global method. If you need to break up up the pathname, for example, a URL like Need some front-end development training? Definition and Usage. The click() method simulates a mouse-click on an element. This method can be used to execute a click on an element as if the user manually clicked on it.

You can make use of Regular Expression with following pattern :

[#]$

 var str1 = "http://google.com/#"; 
        var str2 = "http://google.com/#/dfdjh"; 
        console.log(' str 1 outcome : '+str1.search('[\#]$'));
        console.log(' str 2 outcome : '+str2.search('[\#]$'));

4 Ways JavaScript Can Redirect Or Navigate To A Url Or Refresh , The window.location has multiple methods to help you refresh, may need to use JavaScript to redirect or navigate to another URL. hash: used for single page apps and single page websites; host: the For example, third party scripts cannot trigger a redirect. addEventListener("click", function(e){ e. Please note we are using the substring method with a second arguement the length of the URL. This means that the anchor variable will store a string starting after the value variable (depending on how you set it) and will store the rest of the URL until its end. In case we have a more complex URL, for example:

You can try like this

function findUrl(obj) {
	var url = obj.getAttribute("href");
	var retVal = url.endsWith("#");
	console.log(retVal);
	return retVal;
}


    
<a href="http://www.example.com/#" id="link" onclick="findUrl(this); return false;">Link</a>

Location hash Property, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� JavaScript - Redirect to another URL on Button Click In this code snippet we will learn How to redirect to another URL using JavaScript function, in this example we will take three buttons and redirect to other URLs through button click.

You should use href not hash and attach event listener to it

document.querySelector("a[href='"+hashValue+"']").addEventListener('click',(e) => {
    e.preventDefault()
    //Do some thing here
})

Or better to use Template Strings to avoid problems

document.querySelector(`a[href="${hashValue}"']`).addEventListener('click',(e) => {
        e.preventDefault()
        //Do some thing here 
})

JavaScript if/else Statement, hash host hostname href origin pathname port protocol search assign() reload() replace() The if/else statement is a part of JavaScript's "Conditional" Statements, which Example. If the first <div> element in the document has an id of "myDIV", the source attribute (src) of an <img> element, if the user clicks on the image:. JavaScript Keywords. JavaScript statements often start with a keyword to identify the JavaScript action to be performed. Visit our Reserved Words reference to view a full list of JavaScript keywords. Here is a list of some of the keywords you will learn about in this tutorial:

HTML DOM IFrame src Property, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� The URL also contains parameters or arguments, which we often define dynamically to communicate with elements or objects on a web page. Sometimes we need extract the data that we pass to the URL as parameters. Here I’ll show you how to check if the URL bar contains a given or specified string or text or value using JavaScript indexOf() method.

#GTMtips: Track URL Fragments As Pageviews, Choose History Change as the Event, and set the following condition When someone clicks on a link which only has an anchor in the href, This time, as GTM executes the JavaScript code again, the value of {{Event}} resolves to gtm. the query string (if there is one) and the URL hash (if there is one). Scroll down until you find click variables and enable ALL of them one-by-one, including Click ID, Click URL, etc. After that, refresh the Preview and Debug mode, refresh the web page that you’re working on, and click a link once again (if it opens in the same table, also hold the CTRL or CMD keys pressed while clicking).

Creating hyperlinks, Next up for HTML, we'll return to text semantics and look at some Hyperlinks are one of the most exciting innovations the Web has to offer the end of the URL, preceded by a hash/pound symbol, for example: If you are on a low bandwidth connection, click a link and then a JavaScript building blocks. Instead, if you have to wrap the URL to a new line, find a natural break like a slash, number sign, or other symbol, or if you must, in the middle of a long string of letters or numbers. Use common sense: don't break a URL right after a period or readers might think the period marks the end of the sentence. Click Here and Underlining

Comments
  • What do you want to return true to? The caller of the event listener? (sounds a bit strange, that wouldn't do anything)
  • So the hash needs to be last character or just contained in the url?
  • hash needs to be the last character @nick
  • i just want to execute a function that do something if i click on a url that contains hashtag at the end here for example i put return true