Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari iOS not loading Javascript file?

I have just upgraded my iOS because of the recent security bug, and I find that my website is no longer loading properly on my phone!

I've boiled the problem down to a simplified example...

index.html =

<html>
<head>
    <title>Demo</title>
    <meta name="description" content="Test of basic Javascipt function">
    <script src="demo.js"></script>
</head>
<body class="body">
    <div id="main">
    </div>
</body>
</html>

demo.js =

var i = 0;

function update() {
    'use strict';
    document.getElementById("main").innerHTML = i;
    i = i + 1;
}
setInterval(update, 1000);

On my desktop this produces an iterating count, but when viewed with Safari on my phone (Mobile/12G34 Safari/601.1) the javascript file simply does not load???? The Javascript file doesn't even show up under the "All Resources" tab in my WebInspector which I am tethering via USB.

Suggestions?

Thanks,

Bill


Edit: This question has gotten no response, but I'm still wrestling with this problem. I've found some more information.

Using the Safari WebInspector I've found that the javascript is not loading because...

Blocked script execution in 'http://192.168.133.1/demo.html' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.

Since I'm serving this page off a homespun webserver on an esp8266, this message has inspired me to tune up my HTTP headers. The HTTP headers appear to be fine.

I can't figure out how this html page is getting sandboxed in the first place, and how to 'allow-scripts' so that I can run my script in Safari Mobile.

Any help would be much appreciated.

like image 418
Billy Boy Avatar asked Jul 22 '16 20:07

Billy Boy


People also ask

Why is my JavaScript not working on Safari?

To enable the JavaScript in Safari Browser, you need to open the Settings on your iOS device and then go to the advanced option. Under the advance option, toggle the button of the JavaScript and turn it ON.

Why is my JavaScript not working on iPhone?

On an iPhone, JavaScript should be turned on by default, but if it was disabled at some point, many websites will appear broken in the Safari browser. To enable JavaScript, go into the Settings app on your iPhone, click "Safari," then "Advanced," and swipe the JavaScript button to the right so it appears green.

Why is JavaScript not loading files?

“js file not loading in html” Code Answer Most likely, the problem is that you are including your js file in a head tag or somewhere above your main content. ... You should be able to add the js file in a script tag. The page loads first then JavaScript.


1 Answers

I had exactly same issue when implementing web server functionality for my Arduino project. My web server implementation worked fine with desktop browsers and with Chrome in iOS (9.3.5). I had problems both with Firefox (5.1 (1)) and Safari in iOS. My web pages use scripts including jQuery. Scripts are used for transferring data from Arduino with AJAX/JSON.

The problem was in the examples I used as a basis for my implementation. In those examples, web server's reply to browser's HTTP GET request in OK case doesn't contain anything else than the actual document requested. In that case web browser thinks that the web server is using HTTP/0.9. In addition, Content-Type is missing. Most of the browsers can "guess" what is the Content-Type of the reply although it is not included.

In my case, Safari reported two errors

  1. Blocked script execution in http://myip because the document's frame is sandboxed and the 'allow-scripts' permission is not set.
  2. Sandboxing http://myip/image.jpg' because it is using HTTP/0.9.

The first error causes browser to refuse loading my javascript and also jquery.js. Of course after that AJAX won't work. Firefox in desktop also nagged about not well-formed JSON replies but still worked fine.

I simply changed the implementation so that HTTP replies will always include HTTP version, status code and Content-Type before the actual data. For example, when browser sends request GET / HTTP/1.1, the server doesn't just send the default web page but includes

HTTP/1.1 200 OK
Content-Type: text/html

After that all errors disappeared. It seems new versions of Safari don't like to receive HTTP/0.9 and will sandbox what it gets back.

like image 140
J.Vaattanen Avatar answered Sep 19 '22 22:09

J.Vaattanen