0

I'm trying to get the content of iframe in a javascript alert but, the alert appears empty

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    <title>Untitled</title>
</head>

<style>
iframe {height:200px; width:300px; border:1px solid #000}
</style>

<script>
var iframe = document.getElementById("myFrame");
var iframe_contents = iframe.contentDocument.body.innerHTML;
function newDoc() {
alert (document.getElementById('myFrame').innerHTML) ;
}
</script>

<body>

<iframe src="http://api.adf.ly/api.php?key=c02fe2b360ee4b566a4f1e14d84b279b&uid=3141484&advert_type=banner&domain=adf.ly&url=http://somewebsite.com" id="myFrame">
</iframe><br>

 </br>

 <img src="http://www.giftworksconnect.com/wp-content/uploads/2012/10/download.png" width="100" onclick="newDoc(); return false;" style=" cursor: pointer;" border="0" id="adflink" />


</body>
</html>

any help would be appreciated Regards

Edit:

I'm trying to get the contents of an IFRAME because I'm using Adf.ly Api "http://api.adf.ly/api.php?key=c02fe2b360ee4b566a4f1e14d84b279b&uid=3141484&advert_type=banner&domain=adf.ly&url=http://somewebsite.com" But this api respond with a blank page with the shortend url I want to use the shortened url directly in my site script

3
  • 2
    You can't get the contents of iframes cross domain. If you need to do cross domain page scraping, either use JSONP, or use php/asp to scrape the page. Commented Aug 16, 2013 at 4:46
  • A better question is why are you trying to get the contents of an IFRAME element on a different domain? What exactly are you trying to do (and why?). This would normally constitute "scraping" or creation of a "mash-up", and at the very least you'd normally want to secure permission from the site administrator/owner. If you can get that permission, you can use CORS to overcome your cross-domain issues. en.wikipedia.org/wiki/Cross-origin_resource_sharing Commented Aug 16, 2013 at 6:12
  • I'm trying to get the contents of an IFRAME because I'm using Adf.ly Api "api.adf.ly/…" But this api respond with a blank page with the shortend url I want to use the shortened url directly in my sit script Commented Aug 16, 2013 at 11:35

2 Answers 2

2

I guess your main (parent) page is on another domain. In this case your access to the iframe content is forbidden due to cross-domain restrictions. If you don't have control over the inner page (api.adf.ly/api.php) you can't handle it on with the client-side code on your page.

Sign up to request clarification or add additional context in comments.

Comments

-1

replace Your below line

alert (document.getElementById('myFrame').innerHTML) ;

with the below

alert(document.getElementById('myFrame').src);

Think it will work for You.

1 Comment

thanks for help, but this gets me the source url not the content

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.