Why only in firefox imported javascript source code doesn't run?
The html source code properly exhibits a pop-up box as expected in almost all browsers(chrome,ie11,opera,android,...) but only it fails in Firefox... Can you explain why this happens so? How to make the script in question get work that is loaded from another domain as shown in source code?
---SOURCE CODE---
<head> </head> <body> <script> function processJson(r) { alert("Function Called"); } </script> <script src="http://api.ipinfodb.com/v3/ip-city/?key=a2f2062d64fd705bbb32ce4c44e8ebb508d080990528d7cb4f1a0c5e7ddf5c1e&format=json&callback=processJson"></script> </body> </html>
Zmodyfikowany przez cor-el w dniu
Wszystkie odpowiedzi (7)
Seems to be working here.
data:text/html;charset=utf-8;base64,PGh0bWw+CjxoZWFkPgoKPC9oZWFkPgo8Ym9keT4KCjxzY3JpcHQ+CmZ1bmN0aW9uIHByb2Nlc3NKc29uKHIpIHsgCiAgICBhbGVydCgiRnVuY3Rpb24gQ2FsbGVkIik7CiAgfSAJICAKPC9zY3JpcHQ+CjxzY3JpcHQgc3JjPSJodHRwOi8vYXBpLmlwaW5mb2RiLmNvbS92My9pcC1jaXR5Lz9rZXk9YTJmMjA2MmQ2NGZkNzA1YmJiMzJjZTRjNDRlOGViYjUwOGQwODA5OTA1MjhkN2NiNGYxYTBjNWU3ZGRmNWMxZSZmb3JtYXQ9anNvbiZjYWxsYmFjaz1wcm9jZXNzSnNvbiI+PC9zY3JpcHQ+Cgo8L2JvZHk+CjwvaHRtbD4=
Some thoughts:
(1) It's useful to add a type attribute to your script tags:
<script type="text/javascript">
(2) Do you see any error in the Browser Console (Ctrl+Shift+j) when calling functions in the script file?
(3) What is the character encoding of the host page? You can check using the Page Info dialog. Either:
- right-click the page > View Page Info
- (menu bar) Tools > Page Info
Some encodings can cause compatibility issues with externally included files.
This isn't working for me in Firefox 37.0.
In the Browser Console:
ReferenceError: processJson is not defined
I notice the API is returning
Content-Type: application/json; charset=UTF-8
It's object data and not code. Maybe that is a problem? Can JSON run functions?
data:text/html;charset=utf-8;base64,PGh0bWw+CjxoZWFkPgoKPC9oZWFkPgo8Ym9keT4KCjxzY3JpcHQ+CmZ1bmN0aW9uIHByb2Nlc3NKc29uKHIpIHsgCiAgICBwcm9tcHQoIkZ1bmN0aW9uIENhbGxlZCIsSlNPTi5zdHJpbmdpZnkocikpOwogIH0gCSAgCjwvc2NyaXB0Pgo8c2NyaXB0IHNyYz0iaHR0cDovL2FwaS5pcGluZm9kYi5jb20vdjMvaXAtY2l0eS8/a2V5PWEyZjIwNjJkNjRmZDcwNWJiYjMyY2U0YzQ0ZThlYmI1MDhkMDgwOTkwNTI4ZDdjYjRmMWEwYzVlN2RkZjVjMWUmZm9ybWF0PWpzb24mY2FsbGJhY2s9cHJvY2Vzc0pzb24iPjwvc2NyaXB0PgoKPC9ib2R5Pgo8L2h0bWw+
<html> <head> </head> <body> <script> function processJson(r) { prompt("Function Called",JSON.stringify(r)); } </script> <script src="http://api.ipinfodb.com/v3/ip-city/?key=a2f2062d64fd705bbb32ce4c44e8ebb508d080990528d7cb4f1a0c5e7ddf5c1e&format=json&callback=processJson"></script> </body> </html>
@cor-el,
Sorry but, r u running the code in firefox, for sure? Cos my FF version is 37.0.1 and I bet that doesn't work on it, regardless of what you would type inside function definition for processJson....because the function is not being called at all...And to my sense... Observing the http requests by looking under "Network" tab, I don't see any http requests when I refresh the code...
If this same code works in almost all browsers with no error, and if not works in FF, I think there's a BUG in FF, or some other important issue which I am missing. Still thanks for reply, but I ask you to confirm, which browser gave you that prompt result? Cos you're right, when run your code in chrome and opera, I got the same result as yours, but FF doesn't!
@jscher2000,
I revised the code as per your proposal concerning "text/javascript" definition and "charsef=utf-8" definitions, in case that could have been the bug..but didn't work as I expected. I believe there's really something like a bug. Cos when observed in Ctrl+Shift+J (developer tools) under "Network" tab, I don't even see any cross-domain http requests being sent from my comp(client) to the http://api.infodb.com/.... url. Leave aside the problem of returned response being interpreted as a javascript text, the browser I believe even doesn't recognize the <script src="....">..... It smells like as if FF browser regards this <script> tag as a risk for security? as a cross-domain (infringing same-origin policy) request maybe? maybe this is the case? Am I right? or?
Any help greatly appreciated. Thnx
I don't think the problem is what your code says about the response from the server, the server is sending the content-type header application/json and you can't override that on the client side in Firefox.
Some more reading:
It's certainly possible that this is a change in Firefox, to ignore function calls in JSON objects. Any idea when it stopped working? The API provider says it was tested in Firefox 3.5, which was ages ago... http://www.ipinfodb.com/ip_location_api_json.php