0

I'm trying to read in a list of hrefs before the page loads or on the page load and convert all of the hrefs into iframes that point to the location that the href used to point to. For example:

Take this:

<div id="maincontent" >
<p> </p>
<a href="http://page1.html">page1.html</a><br />
<br />
<a href="http://page2.html">page1.html</a><br />
<br />
<a href="http://page3.html">page1.html</a><br />
<br />
</div>

And replace with:

<div id="maincontent" >
<iframe src="http://page1.html"></iframe>
<iframe src="http://page2.html"></iframe>
<iframe src="http://page3.html"></iframe>
</div>

I'd also like to be able to delete the br and p tags while I am at it. Can someone show me how to read in the content into jQuery and then rewrite it to the page in my new format? Many thanks for your expertise!

user3139728
  • 89
  • 1
  • 4

3 Answers3

1

Something like:

$("#maincontent a").each(function() {
    var src = this.href;
    $(this).replaceWith("<iframe src='" + src + "' />");
});
tymeJV
  • 103,943
  • 14
  • 161
  • 157
0

$('#maincontent a').each(function(){

  var href= $(this).attr('href');
  var iframe = $('<iframe>', {src:href});
  $(this).replaceWith(iframe);

});
Community
  • 1
  • 1
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
0

You could First get the content into a variable.

E.g. var $maincontent = $('#maincontent');

Then use find to get all Elements Matching the "a" tag.

var $links = $maincontent.find("a");

Clean all Elements in existent div.

$maincontent.html("");

Then go through the links and create an iframe per a Tag.

$links.each(function(){
    var href = $(this).prop("href");
    var $iframe =  $("<iframe>").prop("src",href);
    $maincontent.append($iframe);
});

you can also create the html as String in the each to create the html and replace the original html. In the maincontent div.

Scriptlabs
  • 498
  • 3
  • 16