Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In mobile devices' safari, <a href="#" onclick="return false"> doesn't work, why?

In moblie devices' safari, like iphone or ipad, <a href="#" onclick="return false"> doesn't prevent default behaviour, the page was still redirected to '#', why...? Like these html code:

<div style="height:1000px; width:100px"></div>
<br />    
<a href="#" onclick="return false">click me</a>

When click in moblie devices' safari, it goes back to the top of the page...

like image 900
skyworm Avatar asked Apr 19 '12 07:04

skyworm


People also ask

How do I see full URL in Safari on iPhone?

Step to view full URL of links in Safari on iPhone and iPad:Press and hold a link on the webpage until a menu pops up on the screen, and then you can check its full URL on the menu, as shown in the following picture.

Is Safari history shared between devices?

Set up iCloud Safari on Mac On your Mac, choose Apple menu > System Preferences, click Apple ID, then select iCloud in the sidebar > Select and check Safari. Then in the Safari app on your Mac, you can choose History and click Show All History. All browsing history is synced on your Mac.


1 Answers

I had the same problem. It turns out that it is a bug in the iOS 5 version of Safari. Doesn't happen in newer or older versions, or any other browser or platform.

I resolved it by adding preventDefault to the onclick event handler, in addition to the existing return false, like so:

<a href="#" onclick="event.preventDefault(); return false;">click me</a>

Not ideal, but it does solve the problem.

like image 84
SDC Avatar answered Sep 28 '22 02:09

SDC