Strange DOM Exception 18 Security Error

Posted By : Deepak Rawat | 30-Jun-2016

From last few days we were facing a strange issue of “DOM Exception 18” in our ios phonegap application, due to which our app got stuck at some point and the user experience is very bad.

 

    The error which we got was:

 

SecurityError: DOM Exception 18: An attempt was made to break through the security policy of the user agent.


When we debug this we found that it occurs due to a bug in safari as per apple developer forum, according to which safari throws exception in pushState or replaceState which this method is called more than 100 times, but in our case the number of times is not common. To reproduce this issue run this function in safari console:

{window.history.pushState({},””,””)};

 

When this error occurs the browser didn’t change its state since history.replaceState method is throwing error due to which the url gets changed but the browser didn’t get refreshed and hence user will stuck on one page and will not get redirected to other page.


    After some digging we found the solution for this issue for our phonegap application. To resolve this issue just add the below:

$(document).on("mobileinit", function() {
		$.mobile.pushStateEnabled = false;
	});

 

With $.mobile.pushStateEnabled  value as false, the browser does use its history.replaceState method to replace the state of the url and does not maintain the history of pages for the browser.

 

Note: Only use this solution if you are not relying on browser page history management.

 
 
THANKS

About Author

Author Image
Deepak Rawat

Deepak is a Web and Mobile application Sr. Lead Frontend developer and good working experience with JQuery , AngularJS , Javascript and PhoneGap. His hobbies are listening to music and photography.

Request for Proposal

Name is required

Comment is required

Sending message..