iOS Safari/WKWebView Unexpected EOF When Parsing Javascript

I wonder if anyone has seen this one weird problem.


1. You have a javascript file with a string with these three characters: "。\""

2. Your device language is set to Japanese.

3. Your loading an html page (which loads the javascript file) from the file system rather than from an http/https URL


In both Safari and the WKWebView get an error while parsing the javascript file: "SyntaxError: Unexpected EOF".


If you change any of the 3 conditions the error goes away.

1. Put a space between the 。character and the \ character. Everything is ok.

2. Change device language to English. Everything is ok.

3. Load the same page (and javascript) from a http/https URL rather than from the file system.


It also works fine in Desktop Safari.


The problem occurs on both iOS 11 and 12.


The 。is a 3-byte utf 8 character. Other 3-byte charcters (like い) still cause the problem, but others dont. Im not sure what the rule is.







If you want to try it yourself, create two files:

# test.js

(function (w) {

var strings = {

Blah: "。\"",

};

w.strings = strings;

})(window);


# index.html

<!DOCTYPE html>

<html>

<h1>H2I</h1>

<div id="result">THIS SHOULD NOT BE BLANK</div>

<script type="text/javascript" src="test.js"></script>

<script type="text/javascript">

var d = document.getElementById('result');

d.innerText = window.strings.Blah;

</script>

</html>


Load that file (index.html) in Safari on iOS from the filesystem, and switch your device to Japanese, then connect the Safari Dev Tools and reload and you should see the error in the console.

Replies

Does it help if you specify the encoding?

In my case, I use.
webview.evaluateJavaScript("") method can not use other third party javascript like jquery,angular

so i use window.postMessage('','') like this

webview.evaluateJavaScript("window.postMessage('param', '*')")

and in html file,

window.addEventListener("message", function(event) {

    fn_start(event.data)

  });

to solve this problem.

hope help other people.