简体   繁体   中英

Load iframe content with Javascript (or jQuery)

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?

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

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM