Javascript: using DOMParser on mobile devices (iOS 7. *)

I am using angular in my project. But it does not matter)

I need to use DOMParser to check and edit some data in one field ( .Content )

And everything was in order ... I took the iPad 3 with iOS 7 installed and was surprised ...

Why is DOMParser not working on iOS 7 (but working on iOS DOMParser )?

How can I solve this problem on iOS 7? Perhaps there are some workarounds?

Here is part of my code:

 var parser = new DOMParser(); var doc = parser.parseFromString('<div id="fetchContent">' + $scope.news.Content + '</div>', "text/html"); ... $scope.news.Content = doc.getElementById('fetchContent').innerHTML; 

if I delete this code - the application works fine, but with DOMParser it seems that it causes an en error ...

+6
source share
1 answer

Some browsers do not support this feature. To fix this, we can use the following javascript code:

 * inspired by https://gist.github.com/1129031 */ /*global document, DOMParser*/ (function(DOMParser) { "use strict"; var proto = DOMParser.prototype , nativeParse = proto.parseFromString ; // Firefox/Opera/IE throw errors on unsupported types try { // WebKit returns null on unsupported types if ((new DOMParser()).parseFromString("", "text/html")) { // text/html parsing is natively supported return; } } catch (ex) {} proto.parseFromString = function(markup, type) { if (/^\s*text\/html\s*(?:;|$)/i.test(type)) { var doc = document.implementation.createHTMLDocument("") ; if (markup.toLowerCase().indexOf('<!doctype') > -1) { doc.documentElement.innerHTML = markup; } else { doc.body.innerHTML = markup; } return doc; } else { return nativeParse.apply(this, arguments); } }; }(DOMParser)); 

The above script was extracted from mozilla's website here .

+3
source

All Articles