I have code that is in an external file and need to load it via Javascript, however I do not know how to do it. I already tried it in several ways, and apparently it would be better with an iframe, but not that. The idea was to use the load () function of jQuery, but it has one detail: the code is being run locally, so this does not work because of HTTP and similar permissions. So I thought of doing an iframe like this:
<iframe id="iframe" src="script1.html"></iframe>
So I tried this way:
var text = $("#iframe").val();
It did not return anything, so I tagged the file and tried:
$('#iframe').contents().find("html").html();
Still, nothing. So I tried this 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, domains, and ports must match.
I mean, is there no way to load a file locally? I just need a clean file with the codes to load later, I'd even put it directly inside a tag inside the page, but that would get very dirty, since someone else will change the code. Note: This code that should be loaded I refer to is a text document, that is, you can interpret as you want, simply, are several phrases, nothing special about it.