Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window.click event does not fire on IOS safari - JavaScript only

Vanilla javascript -

document.addEventListener("click", function(){ alert('click fired');});

with angular 2 + -

 @HostListener('window: click', ['$event'])
        public iosSafariClick(e: any): void {
         alert('event fired');
        }

None of this method worked on IOS safari on iPad.

Unless I click some button, hyperlink, or any actionable item, Click event is not fired.

My goal is to fire blur event on a 'div element'.

To do so I am trying to check if the any click event fired on HTML body and check if it was on not the 'div element'.

HTML >

<html>
  <body>
    <div id= 'menu'>123...</div>
  </body>
</html>

Angular Component > typescript >

 @HostListener('window: click', ['$event'])
   public iosSafariClick(e: any): void {
     if(e.target.id !== 'menu'){
       this.menu = false;  // to close menu 
     }        
   }

Is there any way using javascript or angular to overcome this hurdle?

like image 879
Nick Thakkar Avatar asked Apr 11 '17 01:04

Nick Thakkar


1 Answers

When using touch based devices, the events issued to the browsers are different. It also gives the developer accurate use cases, to design and develop around.

I would assume that since there is no mouse then there will be no click event.

Taken from MDN: In order to provide quality support for touch-based user interfaces, touch events offer the ability to interpret finger (or stylus) activity on touch screens or trackpads.

Try using: document.addEventListener('touchstart', () => console.log('touch called'));

You can always opt to using function () {} instead of () => {}

See full details: https://developer.mozilla.org/en/docs/Web/API/Touch_events

like image 179
Nico Avatar answered Oct 13 '22 00:10

Nico