javascripthtmlframeview-source

How to set the view source multiple html frames in single viewsource


I have three frames in single frameset html page. when i saw the view source of the page look like below

<HTML>
<HEAD>
 <TITLE>A simple frameset document</TITLE>
 </HEAD>
<FRAMESET cols="20%, 80%">
<FRAMESET rows="100, 200">
  <FRAME src="contents_of_frame1.html">
   <FRAME src="contents_of_frame2.html">
</FRAMESET>
<FRAME src="contents_of_frame3.html">

  </FRAMESET>
</HTML>

but my expectation is i want to display the whole frame source in single page using javascript. thanks


Solution

  • Like this bookmarklet?

    Note:

    1. I use XMP, it is deprecated but should work on most browsers
    2. it will attempt to open a new window so allow it
    3. it assumes the frame contents have <HTML> tags
    4. paste the script into a bookmark URL and execute the bookmark on the page
    5. Tested in Chrome on Windows
    javascript:(function() { 
      var htmlContent=[]; 
      for (var i=0;i<window.frames.length;i++) {
        var fr = window.frames[i], doc=fr.contentDocument || fr.document;
        htmlContent.push(doc.getElementsByTagName("HTML")[0].innerHTML);
      }
      var w=window.open('');
      for (var i=0;i<htmlContent.length;i++) {
        w.document.write('Frame '+i+':<hr/><xm'+'p>'+htmlContent[i]+'</xmp><hr/>')
      }
      w.document.close();
    })()
    

    Testing with this html

    <html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <title>Demo by mplungjan</title>
    <script type='text/javascript'>
    var contents_of_frame1 = '<html><head><title>Frame1</title><script>alert("frame 1")<\/script></head><body>Frame1</body></html>';
    var contents_of_frame2 = '<html><head><title>Frame2</title><script>alert("frame 2")<\/script></head><body>Frame 2</body></html>';
    var contents_of_frame3 = '<html><head><title>Frame3</title><script>alert("frame 3")<\/script></head><body>Frame 3</body></html>';
    </script>
    </head>
      <FRAMESET cols="20%, 80%">
        <FRAMESET rows="100, 200">
          <FRAME src="javascript:top.contents_of_frame1" />
          <FRAME src="javascript:top.contents_of_frame2" />
        </FRAMESET>
      <FRAME src="javascript:top.contents_of_frame3" />
    </FRAMESET>
    </html>
    

    I get this output

    Frame 0:


    <head><title>Frame1</title><script>alert("frame 1")</script></head><body>Frame1</body>

    Frame 1:


    <head><title>Frame2</title><script>alert("frame 2")</script></head><body>Frame 2</body>

    Frame 2:


    <head><title>Frame3</title><script>alert("frame 3")</script></head><body>Frame 3</body>