kareem elzeiny
kareem elzeiny

Reputation: 11

Javascript get Iframe contents

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

Upvotes: 0

Views: 4221

Answers (2)

Ripa Saha
Ripa Saha

Reputation: 2540

replace Your below line

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

with the below

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

Think it will work for You.

Upvotes: -1

amakhrov
amakhrov

Reputation: 3939

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.

Upvotes: 2

Related Questions