0

I need read a external code that comes through a file that is in the same folder of the html file. The problem is that the HTML isn't running in a server, so I can't use the jQuery load() and other solutions. So I think in load the code via an iframe like this:

<iframe id="iframe" src="script1.html"></iframe>

The text in the file is something like this:

One paragraph. Some words
Other paragraph, and more words...

I need get the sentences and throw in a array, but I even can't get the text via jQuery. I'm trying do this:

var text = $("#iframe").val();

But it does nothing, so I tried this inserting the html tags in the file:

$('#iframe').contents().find("html").html();

And nothing again. So, how to do it?

Rafael Almeida
  • 677
  • 7
  • 21
  • 1
    possible duplicate of [How to access the content of an iframe with jQuery?](http://stackoverflow.com/questions/1796619/how-to-access-the-content-of-an-iframe-with-jquery) – D4V1D Mar 29 '15 at 17:29
  • I already saw this solution, but it the code mentioned gives me "undefined", and if I run the code seconds after the page load, the Chrome gives me this 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." – Rafael Almeida Mar 29 '15 at 17:35
  • $($('#iframe').get(0).contentWindow.document).find('body').text(). you can use contentWindow to access to window object of iframe. But your page and iframe must in a same domain with same protocol. – Jerry Mar 29 '15 at 18:11

2 Answers2

0

Did you try using a simple $.ajax post/get call ? Instead of .load, .ajax will process any JS & HTML in script1.html

somehing like :

$.ajax({ type: "POST", url:"script1.html", dataType:"html", success: function(data) { // do what you want here }});

Baptiste

BaptisteC
  • 56
  • 2
0

you can get your value by the following:

 var iBody = $("#iframe").contents().find("body");

//here you have the control over your element (#myContent)
var myContent = iBody.find("#myContent"); //store it in variable here where myContent is the div id you want to get value of..
Vishal Wadhawan
  • 1,085
  • 1
  • 9
  • 11