I'm trying to access an iframe contents from the document it is contained in, but I'm getting the following error message:
Unsafe JavaScript attempt to access frame with URL
file:///home/user/work/project/foo.html from frame with URL
file:///home/user/work/project/index.html. Domains, protocols and
ports must match.
when I run this code:
$('#iframe_id').contents();
As you can see, both files are stored locally in the same folder, so can't figure out why it is giving that error.
Chrome as security measures in place around local files. Meaning browsing local files isn't the same as browsing a web server. I would try hosting them somewhere or install a local web server. There is also some command line switches for chrome to disable the local files security but I have never had much luck with it.
Edit:
comand-line switch as mentioned by El Ronnoco is --allow-file-access-from-files
I think there may be a dirty workaround for this in your development version if you start chrome with the command-line switch --allow-file-access-from-files
Change your url to http://www.your-domain.com/foo.html.
Related
I am creating 2 HTML files that will be stored an an iPhone locally and accessed through a WebView.
I am wondering if it is possible to set localStorage in one file, and get the results of the storage from the other file.
I know that localStorage is accessible from files on the same domain, however, it appears that you can not get the value from a different local file?
I have also tried running this in Safari on the desktop with local files and the same issue occurs.
When you are opening the files locally, i.e. using the file:// protocol, as of now the browsers can not determine what is "same domain" so every file is considered a separate domain. Thus you can not use localStorage when you're opening the files.
Here is some more information on the problem in FireFox: https://bugzilla.mozilla.org/show_bug.cgi?id=507361 . Personally I couldn't find much about Safari on this topic.
You can also look over this: Javascript/HTML Storage Options Under File Protocol (file://) . It might be helpful in your situation.
At this time, local files rendered via WKWebView on iOS 10 share access to the "same domain" with respect to localStorage. Unclear whether this is by design or a bug, however.
I am creating 2 HTML files that will be stored an an iPhone locally and accessed through a WebView.
I am wondering if it is possible to set localStorage in one file, and get the results of the storage from the other file.
I know that localStorage is accessible from files on the same domain, however, it appears that you can not get the value from a different local file?
I have also tried running this in Safari on the desktop with local files and the same issue occurs.
When you are opening the files locally, i.e. using the file:// protocol, as of now the browsers can not determine what is "same domain" so every file is considered a separate domain. Thus you can not use localStorage when you're opening the files.
Here is some more information on the problem in FireFox: https://bugzilla.mozilla.org/show_bug.cgi?id=507361 . Personally I couldn't find much about Safari on this topic.
You can also look over this: Javascript/HTML Storage Options Under File Protocol (file://) . It might be helpful in your situation.
At this time, local files rendered via WKWebView on iOS 10 share access to the "same domain" with respect to localStorage. Unclear whether this is by design or a bug, however.
I am currently learning WebGL. In a call to texImage2D, which is called when a texture has finished loading, I get the following SecurityError:
Uncaught SecurityError: Failed to execute 'texImage2D' on 'WebGLRenderingContext': The cross-origin image at /path/to/texure.png may not be loaded.
However, the file is on the same domain, in fact it is in the same directory as the html file requesting it.
Here is the file layout:
> js
|-> script.js
|-> glUtils.js
|-> sylvester.js
> texture.png
> index.html
And when I look in the F12 console's resource list, the image texture.png is there, fully loaded, and it is 256 x 256. Why does it think I am requesting from another domain?
The solution is that you must be on a local webserver, because file:/// domain requests will not be granted. (Information given by Pointy:
If you serve up the stuff from a local webserver it'll work, because the browser will see those as being from the same domain. Chrome used to have an command-line option to allow it: --allow-file-access-from-files but I don't know if it still works
You're probably getting the error because you're using a file:// based URL.
The solution is to use a simple web server. Open a terminal and type
python -m SimpleHTTPServer
then go to http://localhost:8000 (install python if you're on Windows) or use node.js or possibly even better use devd
Do NOT use --allow-file-access-from-files. This opens your machine to getting hacked through the browser. That would be like turning off your firewall or your virus scanner.
I am creating 2 HTML files that will be stored an an iPhone locally and accessed through a WebView.
I am wondering if it is possible to set localStorage in one file, and get the results of the storage from the other file.
I know that localStorage is accessible from files on the same domain, however, it appears that you can not get the value from a different local file?
I have also tried running this in Safari on the desktop with local files and the same issue occurs.
When you are opening the files locally, i.e. using the file:// protocol, as of now the browsers can not determine what is "same domain" so every file is considered a separate domain. Thus you can not use localStorage when you're opening the files.
Here is some more information on the problem in FireFox: https://bugzilla.mozilla.org/show_bug.cgi?id=507361 . Personally I couldn't find much about Safari on this topic.
You can also look over this: Javascript/HTML Storage Options Under File Protocol (file://) . It might be helpful in your situation.
At this time, local files rendered via WKWebView on iOS 10 share access to the "same domain" with respect to localStorage. Unclear whether this is by design or a bug, however.
I am running a tomcat server and my localhost base domain is :
C:/apache/webapps/ROOT/
My webpage is also present here.
However the javascripts are present in an external location.
D:/something/something
And i am importing them the following way
<script src="D:/something/something/js1.js"></script>
In IE the page loads fine and able to locate the scripts, however with chrome it fails.
On debugging I see that chrome tries to append the following:
http://localhost:8080/D:/something/something
How do i get it to work on chrome without copying the scripts to base location?
For doing anything useful from within the JS code, you'll most likely have to obey the same origin policy. This means you'll have to configure your Tomcat so that it serves the scripts, too.
If you really insist on reading the scripts from local files, that's what file:// URLs do.
You can't do this because you will search for D:/something/something/js1.js in the user/client computer.
You can do it calling (read and print) the file via PHP (D:/something/something/js1.js.php) or any other server side programming language or installing a webserver in your external location to call the file via URL like myCDN.mydomain.com.
EDIT: If you will only work in localhost, use #Pointy and #Konstantin K solutions :)