简体   繁体   中英

How to display iframe with parameters only through javascript

I came across this resource where the.js file pulls out the iframe. Source

The js file:

window.document.write("<iframe src=\"somedomain.com/page.htm\"/>");

However i'm not sure how to add attributes such as width/height/scrolling. Further more i would also like to add an image and link it as well, at the end of the iframe

You can use the same method:

window.document.write("<iframe src=\"somedomain.com/page.htm\" width='300' height='900' ></iframe>");

Note that iframe is not a self closing element tag, however most browser will render correctly. Inside the tag you can enter so alternative content incase the browser doesn't support iframes

can you get the reference to the Frame somehow such as this.. if its the first iframe on the page then you would use index [0]

function removeScroll() { window.parent.frames[0].scrolling="no"; }

try using createElement, the native method

      var tempIFrame=document.createElement('iframe');
      tempIFrame.setAttribute('id','RSIFrame');
      tempIFrame.style.border='0px';
      tempIFrame.style.width='0px';
      tempIFrame.style.height='0px';
      IFrameObj = document.body.appendChild(tempIFrame);

      if (document.frames) {
        // this is for IE5 Mac, because it will only
        // allow access to the document object
        // of the IFrame if we access it through
        // the document.frames array
        IFrameObj = document.frames['RSIFrame'];

Since this started with document.write, just continue it:

document.write("<iframe src=\"somedomain.com/page.htm\"/ width=200 scrolling='no'>");

Note that HTML doesn't actually require quotes for attributes in certain cases (see width ), but they could be added just as with src . Also ' can be used in place of " and it simplifies the escaping (see scrolling ). Please be consistent, the above notes are just informational.

Happy coding.

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