469,639 Members | 1,577 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,639 developers. It's quick & easy.

Iframe load problem

I am trying to load page into iframe in IE7 and change the page bgColor after
the page has loaded.

I tried 2 options:
1. Change the iframe.src from the parent to the new URL.
2. Write div element into the iframe using document.write()
Retrieve the URL using xmlhttp object.
set the div.innerHTML to the xmlHttp.responseText;


The problems:
In option 1 , when trying to access the loaded page in the iframe I get
javascript error access denied.
In option 2 , I can access the page and change the bgColor
but the page is loaded differently in a different look and feel
it seems that the css is not effecting.

Any comments / ideas / other approaches will be welcome.

10'x Eyal
Feb 22 '07 #1
4 3604
acoder
16,027 Expert Mod 8TB
Is the page from your server?

Post your code so someone can pinpoint the problem.
Feb 22 '07 #2
Is the page from your server?

Post your code so someone can pinpoint the problem.
No the page is loaded from the web.
Feb 22 '07 #3
acoder
16,027 Expert Mod 8TB
That is probably why you're getting the access denied error.

As for the css, it is a relative reference, so you will have to make it absolute.
Feb 22 '07 #4
That is probably why you're getting the access denied error.

As for the css, it is a relative reference, so you will have to make it absolute.
Please see the code:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <body> 
  3.     <script>
  4.      var xmlHttp= false; 
  5.      function writeResponseHtml() {
  6.            // if xmlhttp shows "loaded"
  7.           if (xmlHttp.readyState == 4) {
  8.  
  9.                if (xmlHttp.status==200) {
  10.                    // ...some code here...
  11.                       response = xmlHttp.responseText;
  12.  
  13.                    //write the HTML to the iframe  
  14.                        try {
  15.                             frames['contentiFrame'].document.write("<html><head></head><body><div id='iDiv'></div></body></html>")  ;                   
  16.                             frames['contentiFrame'].iDiv.innerHTML = response ;
  17.                         } catch (e2) {
  18.                             ;
  19.                         }                
  20.                } else {
  21.                       alert("Problem retrieving XML data")
  22.                       }
  23.           }
  24.      }
  25.  
  26.  
  27.  
  28.      function option1(url) {
  29.  
  30.       // Option one - use the iframe capability of loading url
  31.       // always works but with no ability to touch the loaded page
  32.  
  33.          frames['contentiFrame'].location.href=url; 
  34.          return true;
  35.       }
  36.  
  37.  
  38.  
  39.       function option2(url){
  40.  
  41.       // Option two - use the the xmlhttp to get the html and write it to the iframe using document.write
  42.       // does not work all the time.
  43.  
  44.           try {
  45.                  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  46.              } catch (e) {
  47.                         try {
  48.                                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  49.                         } catch (e2) {
  50.                                xmlHttp = false;
  51.                         }
  52.                }
  53.              if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  54.                    xmlHttp = new XMLHttpRequest();
  55.              }
  56.  
  57.           xmlHttp.open("GET", url, true);
  58.           xmlHttp.onreadystatechange = writeResponseHtml;
  59.           xmlHttp.send(null);          
  60.       }         
  61.     </script>
  62.  
  63.  
  64.     <div id="contentDivID">
  65.  
  66.        <a href="#" onclick="option1('http://www.ask.com');">option1</a>
  67.        <a href="#" onclick="option2('http://www.ask.com');">option2</a>
  68.        <a href="#" onclick="frames['contentiFrame'].document.body.bgColor = 'red';">bgColor</a>          
  69.  
  70.        <iframe id="contentiFrame" width='100%' height='100%' scrolling='auto' ></iframe>
  71.      </div>     
  72.   </body>
  73. </html>
  74.  
  75.  

Do you have any idea how can I do it ?
Feb 22 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Tobias Åkeblom | last post: by
3 posts views Thread by Stevie_mac | last post: by
12 posts views Thread by wangzx | last post: by
4 posts views Thread by sukhmeet | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.