Problem with JSON.parse with Safari Browser on iPhone/iPad

50 Likes Comment

Lets understand the problem with sample code. Below is the sample Javascript Code

var obj = JSON.parse($('#WoList').html());
alert('hello'); //this alert does not execute on safari browser on iPad

Here the value of $(‘#WoList’).html() is:

"[{"Id":6806,"CompanyId":null,"CreatedByUserId":null,"StartTime":"8/1/2012 9:00:00 AM"}]"

After running this Script code ,We will see this alert(‘hello’) will not be executed.Because the JSON.parse is unable to parse this JSON string here.Problem here is with the JSON string.

Description

The JSON data is valid here but when strings that seems to be like Date string are present within a HTML node, both the iPad and iPhone will automatically create links from these strings. Safari on iOS detects any string formatted like a phone number, Date or address , makes it a link that calls the number. So basically your JSON is getting distorted when you put it into a standard HTML node. Also If I remove the StartTime property from the JSON string, the above code will start working.So the problem is with this StartTime property that exist in our JSON string.

Solution

To fix this, there are some meta tags that should solve the problem

<meta name="format-detection" content="telephone=no">
<meta name="format-detection" content="date=no">
<meta name="format-detection" content="address=no">

After including these meta tags in your page header, this code will start working.

Source: https://techstuffwithbharat.wordpress.com/2014/10/14/problem-with-json-parse-that-exists-with-safari-browser-on-ipad/

About the Author: Oleksandr Sovenko

Back-End Web Developer. Currently I spend my days working as a remote web developer of EstPress

Leave a Reply

Your email address will not be published. Required fields are marked *