Javascript Error in Google Chrome

When a link is clicked in google chrome, the focus event does not fire. All other browsers fire a focal event for links.

<a href=# onfocus="console.log('focus')">Link</a> 

I do not want to attach the onmousedown event, but onfocus.

Does anyone have a workaround idea.

EDIT:

I definitely consider this a mistake, because all other focusing elements cause focus on the click.

Even non-concentrated elements with a tabindex trigger focus on click in google chrome.

 <div tabindex="-1" onfocus="console.log('focus')">div</div> 

I cannot connect to both click and focus , because then onclick other browsers will call this function twice. I cannot detect this functionality because it requires user interaction, and I will not perform detection with a user agent string because it is good.

Using this html:

 <a href=# onfocus="console.log('focus')" onmousedown="console.log('mousedown')">Link</a> 

Is this in any way invalidating the second onmousedown call onmousedown that the function is called twice in browsers without a browser.

EDIT 2:

After some additional testing, <input type=radio> also fails to trigger focus in google chrome. Why in the world it is google chrome like this, while Opera, IE and firefox are all right. What is crazy is that the mobile web browser browser even launches link links when I tried it on my Android device.

+6
source share
3 answers

One work that you could do to avoid double focus events from appearing in production browsers, while getting the focus event that should appear in Chrome, is to check the click event, does the focus on page matter, and if not, fire the focus event.

Using jQuery, this can be done as follows:

 $('a').click(function(e){ if(!$('*:focus').length) $(this).trigger('focus'); }); 

example: http://jsfiddle.net/niklasvh/qmcUt/

+1
source

This really works fine, except that the focus event does not fire just by clicking on the link (try tabbing and you will see the event shooting). I do not think this is a mistake, why not just safely protect and use both?

+3
source

You can use a little hack:

 <a href="#" onfocus="console.log('focus')" onclick="this.focus()">Link</a> 
0
source

Source: https://habr.com/ru/post/890874/


All Articles