<p>I have built a basic data entry application allowing users to browse external content in iframe and enter data quickly from the same page. One of the data variables is the URL.
Ideally I would like to be able to load the iframes current url into a textbox with javascript. I realize now that this is not going to happen due to security issues.</p>

<p>Has anyone done anything on the server side? or know of any .Net browser in browser controls. The ultimate goal is to just give the user an easy method of extracting the url of the page they are viewing in the iframe It doesn't necessarily HAVE to be an iframe, a browser in the browser would be ideal.</p>

<p>Thanks,
Adam</p>

[{'answer_id': 46361, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p>Does this help? </p>\n\n<p><a href="http://www.quirksmode.org/js/iframe.html" rel="nofollow noreferrer">http://www.quirksmode.org/js/iframe.html</a></p>\n\n<p>I only tested this in firefox, but if you have something like this:</p>\n\n<pre><code>&lt;iframe name=\'myframe\' id=\'myframe\' src=\'http://www.google.com\'&gt;&lt;/iframe&gt;\n</code></pre>\n\n<p>You can get its address by using:</p>\n\n<pre><code>document.getElementById(\'myframe\').src\n</code></pre>\n\n<p>Not sure if I understood your question correctly but anyways :)</p>\n'}, {'answer_id': 47190, 'author': 'Jon', 'author_id': 4764, 'author_profile': 'https://Stackoverflow.com/users/4764', 'pm_score': -1, 'selected': False, 'text': '<p>Ok, so in this application, there is an iframe in which the user is supplied with links or some capacity that allows that iframe to browse to some external site.  You are then looking to capture the URL to which the user has browsed.</p>\n\n<p>Something to keep in mind.  Since the URL is to an external source, you will be limited in how much you can interact with this iframe via javascript (or an client side access for that matter), this is known as browser cross-domain security, as apparently you have discovered.  There are clever work arounds, as presented here <a href="http://www.hackszine.com/blog/archive/2007/11/crossdomain_crossframe_javascr.html" rel="nofollow noreferrer" title="Cross-domain, cross-frame Javascript">Cross-domain, cross-frame Javascript</a>, although I do not think this work around applies in this case. </p>\n\n<p>About all you can access is the location, as you need.</p>\n\n<p>I would suggest making the code presented more resilitant and less error prone.  Try browsing the web sometime with IE or FF configured to show javascript errors.  You will be surprised just how many javascript errors are thrown, largely because there is a lot of error prone javascript out there, which just continues to proliferate.</p>\n\n<p>This solution assumes that the iframe in question is the same "window" context where you are running the javascript.  (Meaning, it is not embedded within another frame or iframe, in which case, the javascript code gets more involved, and you likely need to recursively search through the window hierarchy.)</p>\n\n<pre><code>&lt;iframe name=\'frmExternal\' id=\'frmExternal\' src=\'http://www.stackoverflow.com\'&gt;&lt;/frame&gt;\n&lt;input type=\'text\' id=\'txtUrl\' /&gt;\n&lt;input type=\'button\' id=\'btnGetUrl\' value=\'Get URL\' onclick=\'GetIFrameUrl();\' /&gt;\n\n&lt;script language=\'javascript\' type=\'text/javascript\'&gt;\nfunction GetIFrameUrl()\n{\n    if (!document.getElementById)\n    {\n        return;\n    }\n\n    var frm = document.getElementById("frmExternal");\n    var txt = document.getElementById("txtUrl");\n\n    if (frm == null || txt == null)\n    {\n        // not great user feedback but slightly better than obnoxious script errors\n       alert("There was a problem with this page, please refresh.");\n       return;\n    }\n\n    txt.value = frm.src;\n}\n&lt;/script&gt;\n</code></pre>\n\n<p>Hope this helps.</p>\n'}, {'answer_id': 332611, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p>You can access the src property of the iframe but that will only give you the initially loaded URL.  If the user is navigating around in the iframe via you\'ll need to use an HTA to solve the security problem.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/ms536474(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms536474(VS.85).aspx</a></p>\n\n<p>Check out the link, using an HTA and setting the "application" property of an iframe will allow you to access the document.href property and parse out all of the information you want, including DOM elements and their values if you so choose.</p>\n'}, {'answer_id': 332665, 'author': 'Thomas Hansen', 'author_id': 29746, 'author_profile': 'https://Stackoverflow.com/users/29746', 'pm_score': 0, 'selected': False, 'text': '<p>You can use <a href="http://ra-ajax.org" rel="nofollow noreferrer">Ra-Ajax</a> and have an iframe wrapped inside e.g. a Window control. Though in general terms I don\'t encourage people to use iframes (for <em>anything</em>)</p>\n\n<p>Another alternative is to load the HTML on the server and send it directly into the Window as the content of a Label or something. Check out how this <a href="http://ra-ajax.org/samples/Viewport-RSS-Starter-Kit.aspx" rel="nofollow noreferrer">Ajax RSS parser</a> is loading the RSS items in the source which can be downloaded <a href="http://code.google.com/p/ra-ajax/" rel="nofollow noreferrer">here</a> (Open Source - LGPL)</p>\n\n<p>(Disclaimer; I work with Ra-Ajax...)</p>\n'}, {'answer_id': 666832, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': False, 'text': "<pre><code>document.getElementById('iframeID').contentWindow.location.href\n</code></pre>\n\n<p>You can't access cross-domain iframe location at all.</p>\n"}, {'answer_id': 836393, 'author': 'igorsantos07', 'author_id': 102960, 'author_profile': 'https://Stackoverflow.com/users/102960', 'pm_score': 1, 'selected': False, 'text': '<p>HTA works like a normal windows application.<br>\nYou write HTML code, and save it as an .hta file.<br /><br />\nHowever, there are, at least, one drawback: The browser can\'t open an .hta file; it\'s handled as a normal .exe program. So, if you place a link to an .hta onto your web page, it will open a download dialog, asking of you want to open or save the HTA file. If its not a problem for you, you can click "Open" and it will open a new window (that have no toolbars, so no Back button, neither address bar, neither menubar).<br />\n<br />\nI needed to do something very similar to what you want, but instead of <code>iframes</code>, I used a real <code>frameset</code>.<br>\nThe main page need to be a .hta file; the other should be a normal .htm page (or .php or whatever).<br /><br />\nHere\'s an example of a HTA page with  2 frames, where the top one have a button and a text field, that contains the second frame URL; the button updates the field:</p>\n\n<h3>frameset.hta</h3>\n\n<pre><code>&lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;HTA Example&lt;/title&gt;\n        &lt;HTA:APPLICATION id="frames" border="thin" caption="yes" icon="http://www.google.com/favicon.ico" showintaskbar="yes" singleinstance="no" sysmenu="yes" navigable="yes" contextmenu="no" innerborder="no" scroll="auto" scrollflat="yes" selection="yes" windowstate="normal"&gt;&lt;/HTA:APPLICATION&gt;\n    &lt;/head&gt;\n    &lt;frameset rows="60px, *"&gt;\n        &lt;frame src="topo.htm" name="topo" id="topo" application="yes" /&gt;\n        &lt;frame src="http://www.google.com" name="conteudo" id="conteudo" application="yes" /&gt;\n    &lt;/frameset&gt;\n&lt;/html&gt;\n</code></pre>\n\n<ul>\n<li>There\'s an <code>HTA:APPLICATION</code> tag that sets some properties to the file; it\'s good to have, but it isn\'t a must.</li>\n<li>You <em>NEED</em> to place an <code>application="yes"</code> at the frames\' tags. It says they belongs to the program too and should have access to all data (if you don\'t, the frames will still show the error you had before). </li>\n</ul>\n\n<h3>topo.htm</h3>\n\n<pre><code>&lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;Topo&lt;/title&gt;\n        &lt;script type="text/javascript"&gt;\n            function copia_url() {\n                campo.value = parent.conteudo.location;\n            }\n        &lt;/script&gt;\n    &lt;/head&gt;\n    &lt;body style="background: lightBlue;" onload="copia_url()"&gt;\n        &lt;input type="button" value="Copiar URL" onclick="copia_url()" /&gt;\n        &lt;input type="text" size="120" id="campo" /&gt;\n    &lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<ul>\n<li>You should notice that I didn\'t used any getElement function to fetch the field; on HTA file, all elements that have an ID becomes instantly an object</li>\n</ul>\n\n<p>I hope this help you, and others that get to this question. It solved my problem, that looks like to be the same as you have.<br /><br />\nYou can found more information here: <a href="http://www.irt.org/articles/js191/index.htm" rel="nofollow noreferrer">http://www.irt.org/articles/js191/index.htm</a><br /><br />\nEnjoy =]</p>\n'}, {'answer_id': 1245166, 'author': 'Joaquin Cuenca Abela', 'author_id': 141253, 'author_profile': 'https://Stackoverflow.com/users/141253', 'pm_score': 6, 'selected': False, 'text': '<p>I did some tests in Firefox 3 comparing the value of <code>.src</code> and <code>.documentWindow.location.href</code> in an <code>iframe</code>. (Note: The <code>documentWindow</code> is called <code>contentDocument</code> in Chrome, so instead of <code>.documentWindow.location.href</code> in Chrome it will be <code>.contentDocument.location.href</code>.)</p>\n\n<p><code>src</code> is always the last URL that was loaded in the iframe without user interaction. I.e., it contains the first value for the URL, or the last value you set up with Javascript from the containing window doing:</p>\n\n<pre><code>document.getElementById("myiframe").src = \'http://www.google.com/\';\n</code></pre>\n\n<p>If the user navigates inside the iframe, you can\'t anymore access the value of the URL using src. In the previous example, if the user goes away from www.google.com and you do:</p>\n\n<pre><code>alert(document.getElementById("myiframe").src);\n</code></pre>\n\n<p>You will still get "<a href="http://www.google.com" rel="noreferrer">http://www.google.com</a>".</p>\n\n<p><code>documentWindow.location.href</code> is only available if the iframe contains a page in the same domain as the containing window, but if it\'s available it always contains the right value for the URL, even if the user navigates in the iframe.</p>\n\n<p>If you try to access <code>documentWindow.location.href</code> (or anything under <code>documentWindow</code>) and the iframe is in a page that doesn\'t belong to the domain of the containing window, it will raise an exception:</p>\n\n<pre><code>document.getElementById("myiframe").src = \'http://www.google.com/\';\nalert(document.getElementById("myiframe").documentWindow.location.href);\nError: Permission denied to get property Location.href\n</code></pre>\n\n<p>I have not tested any other browser.</p>\n\n<p>Hope it helps!</p>\n'}, {'answer_id': 3611428, 'author': 'Brian Stinar', 'author_id': 142979, 'author_profile': 'https://Stackoverflow.com/users/142979', 'pm_score': 1, 'selected': False, 'text': "<p>I like your server side idea, even if my proposed implementation of it sounds a little bit ghetto.</p>\n\n<p>You could set the .innerHTML of the iframe to the HTML contents you grab server side. Depending on how you grab this, you will have to pay attention to relative versus absolute paths. </p>\n\n<p>Plus, depending on how the page you are grabbing interacts with other pages, this could totally not work (cookies being set for the page you are grabbing won't work across domains, maybe state is being tracked in Javascript... Lots of reasons this might not work.) </p>\n\n<p>I don't believe that tracking the current state of the page you are trying to mirror is theoretically possible, but I'm not sure. The site could track all sorts of things server side, you won't have access to this state. Imagine the case where on a page load a variable is set to a random value server-side, how would you capture this state?</p>\n\n<p>Do these ideas help with anything?</p>\n\n<p>-Brian J. Stinar-</p>\n"}, {'answer_id': 17766493, 'author': 'H4NIO', 'author_id': 1633403, 'author_profile': 'https://Stackoverflow.com/users/1633403', 'pm_score': 2, 'selected': False, 'text': '<p>I use this.</p>\n\n<pre><code>var iframe = parent.document.getElementById("theiframe");\nvar innerDoc = iframe.contentDocument || iframe.contentWindow.document;\n\nvar currentFrame = innerDoc.location.href;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/44359', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4568/']