Getting the document from an iframe

1

How do I create a script that returns with an iframe document. I tried this code but it did not work:


function getDocumento(ifrane){
    return iframe.document;
}
    
asked by anonymous 23.08.2014 / 23:54

1 answer

6

I suggest using a library for this. With jQuery it would look something like:

function getFrameContents(iframe){
    return $(iframe).contents();
}

In the pure javascript version you have to take into account different browsers. Try this:

function getFrameContents(iframe){
   var iframeDocument = iframe.contentDocument || iframe.contentWindow.document
   return iframeDocument;
}

Notice that your code has the argument name of the function with "n" instead of "m".

Please note that if iframe is in a different domain you will have limitations of CORS .

    
24.08.2014 / 00:00