Adrian
Adrian

Reputation: 2291

How to check if a menu link or tab had been clicked on and the user is on the link/page with Javascript?

How can I check if a menu link or tab had been clicked on and the user is on that link/page with Javascript NO JQUERY please?

Upvotes: 0

Views: 155

Answers (1)

Nick Chapman
Nick Chapman

Reputation: 4634

Not too difficult.

first create the following function:

function stopIt(me){
    var loc = me.getAttribute('href');
    if(window.location == loc){
        return false;
    }
    else{
        window.location = loc;
    }
}

then add it as an onclick to your links.

<a href="https://www.google.com" onclick="stopIt(this);">Google</a>

If you're on that page already then it will just stop running. But if not then you will continue on as usual.

Upvotes: 2

Related Questions