JavaScript - Iframe.contentdocument Unsearchable
Hi,
see the following 2 parts of code (altered from http://w3schools.com/js/tryit.asp?fi...ontentdocument) Code: <body> <iframe src="hoi.htm" id="frame1"></iframe> <br /><br /> <script type="text/javascript"> function getText() { var x=document.getElementById("frame1").contentDocument; alert(x.forms); } </script> <input type="button" onclick="getText()" value="Get text" /> </body> Code: <body> <iframe src="http://web.qq.com/" id="qqwin" style="height:300px;"></iframe> <br /><br /> <script type="text/javascript"> function getDoc() { var y=window.document.getElementById('qqwin').contentDocument; alert(y.forms); } </script> <input type="button" onclick="getDoc()" value="Get doc" /> </body> hoi.htm is an existing empty document. the first part of code works, but as soon as I change the iframe source into http://www.qq.com or http://www.w3schools.com no alert is shown anymore. the contentDocument I get though: when I change Code: alert(y.forms); into Code: alert(y); , I get an alert saying object HTML document basically: when I use an empty test document as iframe source, I can get everything I want from the contentDocument, but when I use an external source I can only get the contentDocument itself as an object, but not anything thats in there my question is 'how do I get elements from an externally sourced contentDocument?' Kind regards, Klaas Similar TutorialsI needed to know if a remote Page was completely loaded in an iframe. If I try to use framename.contentDocument.readyState=='complete' I get an permissions error. However if a create a local Page such as: ----------------------------------------------- <html> <body> <iframe src="http//:somewhere.com" > </iframe> </body> </html> --------------------------------------- I can check whether the local Page, hence the remote Page I was interested in is loaded. Will this hack go away or does it conform to the standard? Hi all, I hope you can help I have some code which: 1) Opens a URL (e.g. google.com) in a hidden iframe, then 2) Redirects the visitor to a different URL (e.g. yahoo.com) after it has loaded... This works exactly as I need. What I'd like to do is the following: 1) Open a URL (e.g. google.com) in a hidden iframe, then 2) Redirect the visitor to a different URL (e.g. yahoo.com) in a full-screen iframe 3) The address bar of the full-screen iframe should be my domain (obviously!) Here's what I have so far... Hidden iframe -> New URL Code: Code: <html> <body> <script type="text/javascript"> var page_body = document.getElementsByTagName('body'); var myframe = document.createElement('iframe'); var id = Math.random(); myframe.setAttribute('id', 'myFrame'+id); myframe.src = 'http://google.com'; //First open Google in hidden iframe myframe.setAttribute('frameborder', '0') myframe.setAttribute('width', '0'); myframe.setAttribute('height', '0'); if (myframe.attachEvent) { myframe.attachEvent('onload', function() { top.location = 'http://yahoo.com'; //Once loaded redirect to Yahoo -- but the address bar shows yahoo.com -- I want the address bar to display mydomain.com/page.html }); } else if (myframe.addEventListener) { myframe.addEventListener('load', function() { top.location = ('http://yahoo.com'); }, false); } page_body[0].appendChild(myframe); </script> </body> </html> I have an example of some code which displays the full screen iframe, but my problem is... ...I can't figure out how to redirect to this after the hidden iframe has loaded Here's an example of how I want the redirect to display -- after the initial hidden iframe has loaded: Code: <html> <head> <script type="text/javascript"> function sendParams() { document.body.style.overflow = "hidden"; } </script> </head> <body> <!--Open Yahoo.com in a full screen iframe, and disable extra scrollbar --> <iframe height="100%" width="100%" frameBorder="no" onload="sendParams()" src="http://yahoo.com"></iframe> </body> </html> I've been beating my head against the wall today trying to figure this out, and I'm fresh out of ideas. If you can help, you'll have some serious gratitude and good karma thrown your way! Best wishes, and thanks in advance, Paul Is there a way to resize an iframe dynamically so that you never get the scroll bar and essentially hide that there is an iframe? Better integration really. Basically I want to iframe a forum into my site so that the design down the sides and top which my friend does using iweb are not messed with. We have a central area which can be longer or shorter depending on the forum. Hi there everybody, I've got this problem which I can't solve myself... I've got a website which has 3 colums and is hosted on domain1.com. in the right column I've got an Iframe which loads content from domain2.com . this content are some textboxes and a datepicker tool. if I press the 'search'-button in this Iframe I would like to refresh the mainpage (from domain1.com) so that another Iframe appears in the middle column. in this column i would like to load a new form from domain2.com which contains the values that I've put in in the form from the right Iframe. Unfortunally I can't get this to work... Will somebody please help me? I have used iframes for my site and found various javascripts so when a person clicks on say the audio page he http://www.krillmeed.com/index.html it takes them to the audio page but will load it into the index.html page. The problem is, especially with search engines, to point to that page, this is what the URL looks like to send someone to that page: http://www.krillmeed.com/?frame=0&sr...m%2Faudio.html which is not very clean at all, this is the javascript that i found that at least works: Child iframe script: Code: <script type="text/javascript"> (function(){ var qstr = '?frame=0&src=' + encodeURIComponent(location.href), lre = new RegExp('^' + location.protocol + '//' + location.hostname + '(()|(/)|(/index.html)|(/index.php))(()|(\\' + qstr + '))$'); if (!lre.test(parent.location.href)){ top.location.href = '/' + qstr; } })(); </script> Parent iframe Script: Code: <script type="text/javascript"> (function(){ function getQval(n) { if(typeof n !== 'string'){ return null; } var r = new RegExp('[?&;]' + n + '=([^&;#]*)'), m = location.search; return (m = r.exec(m))? unescape(m[1]) : null; } var f = getQval('frame'), s = getQval('src'); if(f && frames[f] && s && s.indexOf(location.protocol + '//' + location.hostname + '/') === 0){ frames[f].location.href = s; } })(); </script> Is there a "cleaner" way of doing this? This was an old code, i have yet to find a more modern one. Thank you in advance. Hi, Not sure if I worded that question right. I have a web page which will display another web page in an iframe. But the content inside the iframe may change while the user interacts with it so I need to be able to resize the iframe height from code on the page inside the iframe. Any tips on how I can do that? I am using php and javascript. Thanks. Hello to all and thanks in advance for my help. I know this has been posted in different places on the web but I have found no solid solution. Here is my problem: I have an iframe the loads up a page on a remote server (out of my hands) and when I send certain information to it, the page redirects my entire site to their 404 page. I need to prevent this from happening so I engaged in much research and found this... temporary fix: Code: <script> // No redirection! var prevent_bust = 0 window.onbeforeunload = function() { prevent_bust++ } setInterval(function() { if (prevent_bust > 0) { prevent_bust -= 2 window.top.location = 'http://www.mysite.com/my404.html' } }, 1) </script> What this code does, is that every time the unload of a page happens, it will redirect me to my404.html and this does solve the problem of the iframe trying to redirect my entire site but anytime a user tried to go to a new website by typing it in the address bar, they are redirected to my404.html. Is there another solution for me so that the page within the iframe cannot redirect the parent page? I have an iframe that is a specific height and width. I want to detect whenever the the iframe is no longer exactly the height i set it to. If a user has CSS turned off for example the iframe will be in default height and width. I want to detect that. This is how you'd normally do it but this only returns the original size (try with 'no style' in Firefox): Code: function getSize() { alert('height is now '+document.getElementById("Iframemain").height) alert('height is now '+document.getElementById("Iframemain").width) } Is it possible? Hi I have a page with 3 iFrames, showing different content. iFrame1: Here is a navigation php page. Click on a menu item and it loads a php page in iFrame2 iFrame2: Here is a list of items from a database. iFrame3: I load 3 different php pages here. (A) is the main showing page. (B) and (C) is a record manipulation page, that does some editing in the database and then redirects to (A). When the (A) php page loads, it should reload iFrame2 I have tried to put something like: <body onLoad=parent.location.reload()> and put it on the (A) php page. This will reload the entire page, which works. However, it results in a loop of reloads, as the (A) iFrame file will load with the entire page reload... So, I was looking for the right code to put, like onLoad=iFrame2.location.reload() However, I can't seem to find the correct code, so I hope any of you can help me... Hello.... I've 2 iframes. And i want to change the css class of second iframe anchor tag from the first. Code: <div id="top-navigation" title="top-navi"> <ul><li id="homePageLink" class="first"> <a id="homePageTab" class="navigation-font" href="</a> </li> </ul> </div> In the above code how can i change the class="navigation-font" from the first iframe? Please help.... Hello Experts I need your advice. I dont know if this is the write place to post my query. But I have an assignment due today. I have got everything working except an iframe that is driving me nuts. the question on the assignment asks to insert a command to write the HTML code <iframe src='weekday.htm'></iframe> to the webpage where weekday is the text string returned by the weekDay() function. The iframe should display the daily schedules that are stored in the sunday.htm through saturday.htm file. The weekDay() function is located in an external .js file that I have already linked to my webpage. Another thing is there is no weekday.htm file instead there are files sunday.htm through saturday.htm that have the daily schedules to de displayed in the iframe. I know that I should some link the weekday.htm file to the weekDay() function. This is what I have done so far: <h2 id="title">Today at the Union</h2> <p> <script type="text/javascript"> /* Dispaly the daily schedule in an inline frame. Display schedules are stored in the files sunday.htm through saturday.htm */ <iframe src='weekday.htm'> var weekday = weekDay(); document.write(weekday); </iframe> </script> </p> Please advice as to how to get this thing working. I am not an expert( I am still learning.) I tried my best (spent several hours breaking my head on this). So any help is much appreciated. Thank you for your time does not work in iframe, the error described he http://groups.google.com/group/ie7-j...73a3e?lnk=raot if I do correction as suggested then the error dissapears, but does not work (hover). Anyone found solution for that ? Hi Am new to javascript. I have a requirement where user clicks on a image which should direct to iframe. Please follow 2 scripts and please advice how to open iframe link instead of link in window.open Image code: <img src = "../samples/images/Edit3.png" alt = "Edit Status" height = 13 width = 13 onclick = "window.open('../cgi-bin/cognos.cgi?b_action=cognosViewer&ui.action=run&ui.object=%2fcontent%2ffolder%5b%40name%3d%27NARM%27% 5d%2fpackage%5b%40name%3d%27TM1_NARM_RISK_DASHBOARD_CREDIT_FINANCE%27%5d%2ffolder%5b%40name%3d%27Cre dit%20Finance%20Overlay%20Reports%27%5d%2freport%5b%40name%3d%27%23%2060%2b%20Rate%27%5d&ui.name=%23 %2060%2b%20Rate&run.outputFormat=&run.prompt=true','KPIDetail','width=600,height=350,scrollbars=yes,left=300,top=300')"> Iframe code: <iframe src="http://tm1lab.us.hsbc/TM1Web/TM1WebMain.aspx?AdminHost=tm1lab.us.hsbc&TM1Server=narm_risk_dashboard&action=OpenObject&type=Webshe et&value=Applications/CF_Commentary/H60Rate" width="100%" height = "100%" scrolling=no > </iframe> Above is the image of my situation, what I need is for the captcha's to be loaded perfectly and displayed to the user as shown in the image with the green box around it, I currently have a script to set focus to the text field however it is kind of messy for some reason and ends up leaving it looking like the frame in the red box in the image. Here is what I have been using: Code: if (( document.URL.indexOf("cast_skills") != -1 ) || ( document.URL.indexOf("security_prompt") != -1 ) || ( document.URL.indexOf("joinraid") != -1 )){ if ( document.forms.length > 0 ) { document.forms[0].elements[1].focus(); } } This works fine since on each of the pages it is required there is only 1 form. However when loading smaller frames it causes some problem. The next question I have to ask is, is it possible to have a script that when the enter key is pressed inside frame 1, automatically move the focus on to the text field in frame 2? Noting that the page inside the frames is not my own and I cannot do anything with it. Any help would be appreciated with this matter, Many thanks. Hello, I've lately brought a new search engine scirpt however I was wonder how i could grap what ever the url is being viewed in the iframe, in order to create a hyperlink button which wil take the user to what ever is displayed in the iframe, but without my frame? Very simplilar to the 'remove frame' link that google images has on it's search result pages. I firstly thought that a PHP code would have being able to do this. However after being told my a number of PHP experts I relised that Java was the way forward. Therefore I would love any help what so ever in help me to carry out this task. I've attached the search page from my site with the iframe located. However i've had to change the file from PHP to txt. Many Thanks Hi there, I have a very strange issue using dynamic scripting and iframe. It looks like when when iframe is loaded (i.e. onload method is triggered) all js objects that were sent to iframe before disappear? To clarify... I set iframe.scr from java script that caused it to connect to the server. After connection is established I start sending JS code to it (including some JS objects). At some point connection to iframe is closed (and onload method is called). Later on I reconnect (i.e. set src property again), and try to issue method calls on the JS objects that were sent in earlier session. None of these methods get triggered. Did anyone experienced similar problems? Is there a workaround? (I.e. is there any way I can send register JS Objects globally when sent through iframe?). Thanks for all your help, Ed I have an iframe. I load a page within it. The page I load is a php page that I build with a table with the id of 'readmail_table'. My iframe is 'readmail_frame'. Someone clicks on a message title outside of the iframe and I load the contents of the message in the iframe from the database in my table. Sometimes this takes longer than others depending on how large the message is. What I am doing here is letting the contents load then calculating the width and height of the table and then increasing the iframe's size so there are no scrollbars. This works 98% of the time but I ran into an issue where the table does not exist yet in the iframe and the JS code errors out. So what I did then was add a local var, set it to false then had a while loop check for the table and once it finds it, set the var to false and run my code. Again, mixed results. Sometimes this works then others I get that the table id I am looking for is null but my if statement in the while loop is checking to see if != null line 159 is where it is breaking The error I am getting in firebug is Code: window.frames.readmail_frame.document is null Line 159 Code: 156 var temp = false; 157 while ( !temp ) 158 { 159 if ( window.frames[ "readmail_frame" ].document.getElementById( 'readmail_table' ) != null ) 160 { 161 //height of the table that holds the actual message 162 var tempheight = window.frames[ "readmail_frame" ].document.getElementById( 'readmail_table' ).clientHeight; 163 //height of the from, to, subject and date section 164 var tempheight2 = window.frames[ "readmail_frame" ].document.getElementById( 'readmail_heading' ).clientHeight; 165 var tempwidth = window.frames[ "readmail_frame" ].document.getElementById( 'readmail_table' ).clientWidth; 166 document.getElementById( 'readmail_frame' ).style.width = tempwidth + 'px'; 167 //add up the table for the message, table for from, to, date and icon for print and some extra to ensure enough room 168 document.getElementById( 'readmail_frame' ).style.height = ( tempheight + tempheight2 + 130 ) + 'px'; 169 temp = true; 170 } 171 } I have also tried Code: if ( window.frames[ "readmail_frame" ].document.getElementById( 'readmail_table' ) ) and Code: if ( window.frames[ "readmail_frame" ].document ) Like I said, most of the time this works exactly how I need it. I just need a fool proof way to determine that this table is in fact on the page. I am not sure why it works sometimes and not others since I am looping until it is there Thank you for any help with this. Hey All, I've been poking away at what should be very simple code, but is becoming a nightmare. I have an iframe that the user uses to enter certain information, the iframe loads contiguous urls that do server side scripting and store information, so a total of 12 php files are run through until the user comes to the end of the information gathering session. I then want to give them the option to go back and review the information they gave me, OR submit the information (which I already have anyways). All I want the submit button on this last document to do is to set the style of the div that contains the iframe to display='none'. I have tried everything I can think of to do it, but I can seem to get the darn thing to call an element from the parent window on the inside. I've used "window.parent" and "parentNode" and they absolutely don't work. Any help would be greatly appreciated. On my page there's an iframe containing a div which I've hidden with code on the actual iframe page. I want the iframe content only to be accessible by clicking a button on the main page. Here's the main parts of the code: Within the iframe: Code: <html> <head> <script language="javascript"> <!-- var state = 'none'; function showhide(layer_ref) { if (state == 'block') { state = 'none'; } else { state = 'block'; } if (document.all) { eval( "document.all." + layer_ref + ".style.display = state"); } if (document.layers) { document.layers[layer_ref].display = state; } if (document.getElementById &&!document.all) { hza = document.getElementById(layer_ref); hza.style.display = state; } } //--> </script> </head> <body onload="showhide('div')" style="display: none;"> <div id="div" name="div"> Some Content... </div> </body> </html> And on the main page: Code: <html> <head> </head> <body> <div id="div1"> <iframe onload="iFrameHeight()" id="blockrandom" name="iframe" src="http://somewhere.com/" width="100%" height="500" scrolling="auto" align="top" frameborder="0" class="wrapper"> This option will not work correctly. Unfortunately, your browser does not support inline frames.</iframe> </div> <div id="div2"> <input type="button" value="Show/Hide Search" onclick="document.getElementById('blockrandom').contentWindow.showhide('div')"> </div> </body> </html> The page hides successfully but I can't get it to show again from the button on the main page. If there's something wrong with my code or if there was a way that the iframe page could check if it was loaded on the right page or not would be great although i'm not sure if that's possible. Any solutions or alternatives are welcome. Thanks. P.S. This is on Joomla CMS, I don't think it should make a difference but correct me if I'm wrong. Hello I need to get text from an iframe. I tried this example (I've found it on this board): Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Example</title> <style type="text/css"> </style> <script type="text/javascript"> // <![CDATA[ window.onload = function() { // treat iframe_window as if it were your iframes window (which it is) var iframe_window = window.frames["my_iframe"]; // compare the following line to: window.document.getElementsByTagName("body")[0].innerHTML; var iframe_body_html = iframe_window.document.getElementsByTagName("body")[0].innerHTML; alert(iframe_body_html); } // ]]> </script> </head> <body> <iframe name="my_iframe" id="my_iframe" src="example.html"></iframe> </body> </html> but it works only with window.onload event. I need to get this text dynamically (e.g. on click). could you give me some example? Regards. |