Can Javascript disable HREF's first click?

JavaScript and client side scripting.

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can Javascript disable HREF's first click?

Post by simonmlewis »

We have an issue that is on another thread, but that thread is not totally related to this problem.

On a dropdown menu, on certain devices their 'standards' are not to identify dropdowns, and if they have a HREF tag assigned, the dropdowns work, but goes straight away to the page of the link.

Apple devices do not to it. And on Desktop, they operate via hover. It seems to mostly do this on Android. We are told their standards don't take note of the hover part.

So a plan B would be of the @media width was 768 or below, showing a mobile menu, to disable the HREF on the first tap/click, but enable it on the second.

Is this possible to do??

The only issue might be, where some of the links in the menu are not dropdowns.

Code: Select all

<div class='navigation' id='menu'>
<div class='navitem'><a href='/'><i class='fa fa-home'></i></a></div>

  <div class='navitem'>
  <a href='/contact-us'>Support <i class='fa fa-angle-down' aria-hidden='true'></i></a>
 <div class='navitem-dropdown'>
<a href='/faq'>FAQs</a>
 <a href='/the-experience'>The Experience</a>
<a href='/videos'>Support Videos</a>
 <a href='/contact-us'>Contact</a>
 </div></div>
 
<div class='navitem'><a href='/about'>About Us</a></div>  
<div class='navitem'><a href='/blog'>Blog</a></div>
<div style='clear: both' ></div>
</div>
<div style='clear: both'></div>
</div>
Here is an example.

So how can I disable the HREF on click on the Support dropdown, and have it fire on the second - but NOT do that on the others?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can Javascript disable HREF's first click?

Post by requinix »

I'd avoid putting a link there entirely. Conceptually it's annoying: clicking the "Support" should get me a menu*, but clicking "Support" should also go to the support page? You have the contact link in the submenu anyways.

But to answer the question, yes. Attach a click event handler using your Javascript framework and do a event.preventDefault() if the menu is closed.

* Yes, you said it works on hover, but that's an increasingly less used behavior as it doesn't match up with more common (ie, desktop) interfaces.
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can Javascript disable HREF's first click?

Post by simonmlewis »

So what is the answer??
You have a menu along the top, with them all working on hover (like most sites nowadays). But on a Desktop you click the link for say "Support" and it goes to the contact page. But on an Android device, you tap it to expand the menu and it goes to the Contact page.

That's the annoying bit. And that is what I am trying to overcome, without recoding the whole flipping thing.

Surely there is a fix for this android issue where it ignores the hover. iPad and iPhone don't ignore it. you tap on the top level and it expands, tap it again and it goes to the page.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can Javascript disable HREF's first click?

Post by simonmlewis »

Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can Javascript disable HREF's first click?

Post by requinix »

simonmlewis wrote:So what is the answer??
The answer is what I said in my post. Did you read the whole thing? I did tell you not to use the link, but then I said something else - and that link you gave said the same thing too.
simonmlewis wrote:But on a Desktop you click the link for say "Support" and it goes to the contact page.
Unless it's a menu.
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can Javascript disable HREF's first click?

Post by simonmlewis »

But we need the text in that top menu to be links too. It really works for us and seen that on so many websites.
And on iOS and others, it's fine. Just that on Android it's a pain as it ignores what others seem to do correctly.
We cannot move those top links.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can Javascript disable HREF's first click?

Post by requinix »

And I've seen other websites that don't do that. But whatever, then don't remove them.
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can Javascript disable HREF's first click?

Post by simonmlewis »

So are you saying there is no JS that will be able to ignore the first HREF click on a specific link, but enable it after?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can Javascript disable HREF's first click?

Post by requinix »

No, I'm telling you (for the second time) to read my first post.
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can Javascript disable HREF's first click?

Post by simonmlewis »

But to answer the question, yes. Attach a click event handler using your Javascript framework and do a event.preventDefault() if the menu is closed.
I don't know how to do this. If this is what you are referring to.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can Javascript disable HREF's first click?

Post by requinix »

Yes, that is what I'm referring to.

You using any Javascript framework? You know how to use it to get events, like onclick or onchange?
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can Javascript disable HREF's first click?

Post by simonmlewis »

Vaguely with onclick and onchange. Really vague.
I'm using a bit of Javascript.

That event I looked at, disables the link on the first click, but I want it to enable the dropdown on a mobile device. Concerned that it might disable everything on the first click.

http://www.regatta.com/
This site does what I want. And I don't believe they have two menus (desktop and mobile). Just one. Top levels link.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can Javascript disable HREF's first click?

Post by requinix »

Actually what they're doing is using a block-level element to cover up the link. I forgot to check but it didn't seem possible to click the "link" when the menu was open, so it's not quite the same thing as what you're trying to do.

You're using Javascript to show the menu, right? What does that look like?
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can Javascript disable HREF's first click?

Post by simonmlewis »

I've PMd you. Reasons will be apparent.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can Javascript disable HREF's first click?

Post by requinix »

Obviously you can't use :hover for mobile, so that means co-opting another mechanism for the menu. That'd be touch/click, which must add a class to the item to indicate the faked hover state.

Amend your CSS so that the menu opens up for both :hover and for some other class, like "opened" or whatever you want. Then use Javascript to add a touchstart event listener that goes like

Code: Select all

if (menu does not have class) {
	add class
	return false
}
So when the user touches the menu item, it adds the class (and the menu appears) and the event stops (and doesn't trigger the link). You'll probably also want to remove the class from any other opened menu.

For events use addEventListener. You should be able to add the listener on the menu rather than on the link itself.
Post Reply