1
I have a code that is in an external file and I need to load it via Javascript, but I don’t know how to do this. I have tried several ways, and apparently the best would be with an iframe, but not even that. The idea was to use jQuery’s load() function, but it has one detail: the code is being run locally, so this does not work due to HTTP permissions and the like. So I thought I’d make an iframe like this:
<iframe id="iframe" src="script1.html"></iframe>
So I tried it like this:
var text = $("#iframe").val();
You didn’t call me back, so I tagged the file and tried it like this:
$('#iframe').contents().find("html").html();
Still, nothing. So I tried that way:
var iBody = $("#iframe").contents().find("body");
var myContent = iBody.find("#myContent");
The problem is that Chrome returns an error:
Uncaught Securityerror: Failed to read the 'contentDocument' Property from 'Htmliframeelement': Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, Omains, and ports must match.
That is, is there no way to load a file locally? I just need a clean file with the codes to upload later, I’d even put it directly inside a tag inside the page, but that would be much dirty, since someone else will change the code. Note: This code that should be loaded to which I refer is a text document, IE, you can interpret as you like, simply, are several sentences, nothing special about it.
When you say "external file" you mean another site or file other than the one where you have the iframe (location, on the same server)?
– Sergio
It’s in the same folder as the HTML file, and the files aren’t being run on a server, it’s just double-clicking "index.html".
– Rafael Almeida