简体   繁体   English

如何获取iframe的当前位置?

[英]How do I get the current location of an iframe?

I have built a basic data entry application allowing users to browse external content in iframe and enter data quickly from the same page. 我已经构建了一个基本的数据输入应用程序,允许用户浏览iframe中的外部内容并从同一页面快速输入数据。 One of the data variables is the URL. 其中一个数据变量是URL。 Ideally I would like to be able to load the iframes current url into a textbox with javascript. 理想情况下,我希望能够使用javascript将iframe当前网址加载到文本框中。 I realize now that this is not going to happen due to security issues. 我现在意识到,由于安全问题,这不会发生。

Has anyone done anything on the server side? 有人在服务器端做过什么吗? or know of any .Net browser in browser controls. 或者知道浏览器控件中的任何.Net浏览器。 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. 最终目标是为用户提供一种简单的方法来提取他们在iframe中查看的页面的网址。它不一定是iframe,浏览器中的浏览器将是理想的。

Thanks, Adam 谢谢,亚当

I did some tests in Firefox 3 comparing the value of .src and .documentWindow.location.href in an iframe . 我在Firefox 3中做了一些测试,比较了iframe.src.documentWindow.location.href的值。 (Note: The documentWindow is called contentDocument in Chrome, so instead of .documentWindow.location.href in Chrome it will be .contentDocument.location.href .) (注意: documentWindow在Chrome中称为contentDocument ,因此Chrome中的.documentWindow.location.href不是.contentDocument.location.href 。)

src is always the last URL that was loaded in the iframe without user interaction. src始终是在没有用户交互的情况下在iframe中加载的最后一个URL。 Ie, it contains the first value for the URL, or the last value you set up with Javascript from the containing window doing: 即,它包含URL的第一个值,或者您从包含窗口使用Javascript设置的最后一个值:

document.getElementById("myiframe").src = 'http://www.google.com/';

If the user navigates inside the iframe, you can't anymore access the value of the URL using src. 如果用户在iframe内导航,则无法再使用src访问URL的值。 In the previous example, if the user goes away from www.google.com and you do: 在前面的示例中,如果用户离开www.google.com,您执行以下操作:

alert(document.getElementById("myiframe").src);

You will still get " http://www.google.com ". 您仍然可以获得“ http://www.google.com ”。

documentWindow.location.href 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. documentWindow.location.href仅在iframe包含与包含窗口位于同一域中的页面时可用,但如果它可用,则它始终包含URL的正确值,即使用户在iframe中导航也是如此。

If you try to access documentWindow.location.href (or anything under documentWindow ) and the iframe is in a page that doesn't belong to the domain of the containing window, it will raise an exception: 如果您尝试访问documentWindow.location.href (或documentWindow.location.href下的任何documentWindow )并且iframe位于不属于包含窗口的域的页面中,则会引发异常:

document.getElementById("myiframe").src = 'http://www.google.com/';
alert(document.getElementById("myiframe").documentWindow.location.href);
Error: Permission denied to get property Location.href

I have not tested any other browser. 我没有测试任何其他浏览器。

Hope it helps! 希望能帮助到你!

document.getElementById('iframeID').contentWindow.location.href

您根本无法访问跨域iframe位置。

I use this. 我用这个。

var iframe = parent.document.getElementById("theiframe");
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

var currentFrame = innerDoc.location.href;

I like your server side idea, even if my proposed implementation of it sounds a little bit ghetto. 我喜欢你的服务器方面的想法,即使我提议的实现它听起来有点贫民窟。

You could set the .innerHTML of the iframe to the HTML contents you grab server side. 您可以将iframe的.innerHTML设置为您抓取服务器端的HTML内容。 Depending on how you grab this, you will have to pay attention to relative versus absolute paths. 根据你如何抓住这个,你必须注意相对路径和绝对路径。

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.) 另外,根据您抓取的页面与其他页面的交互方式,这可能完全不起作用(为您抓取的页面设置的cookie不能跨域工作,也许Javascript中正在跟踪状态...有很多原因这可能行不通。)

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? 想象一下,在页面加载变量被设置为服务器端的随机值的情况下,您将如何捕获此状态?

Do these ideas help with anything? 这些想法对任何事都有帮助吗?

-Brian J. Stinar- -Brian J. Stinar-

HTA works like a normal windows application. HTA的工作方式与普通的Windows应用程序类似
You write HTML code, and save it as an .hta file. 您编写HTML代码,并将其另存为.hta文件。

However, there are, at least, one drawback: The browser can't open an .hta file; 但是,至少有一个缺点:浏览器无法打开.hta文件; it's handled as a normal .exe program. 它作为一个普通的.exe程序处理。 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. 因此,如果您将.hta链接放到您的网页上,它将打开一个下载对话框,询问您是否要打开或保存HTA文件。 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). 如果它不是你的问题,你可以点击“打开”它将打开一个新窗口(没有工具栏,所以没有后退按钮,既没有地址栏,也没有菜单栏)。

I needed to do something very similar to what you want, but instead of iframes , I used a real frameset . 我需要做一些与你想要的非常相似的东西,但我使用的是真正的frameset ,而不是iframes
The main page need to be a .hta file; 主页面必须是.hta文件; the other should be a normal .htm page (or .php or whatever). 另一个应该是正常的.htm页面(或.php或其他)。

Here'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; 这是一个包含2个帧的HTA页面的示例,其中顶部有一个按钮和一个文本字段,其中包含第二个帧URL; the button updates the field: 按钮更新字段:

frameset.hta frameset.hta

<html>
    <head>
        <title>HTA Example</title>
        <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"></HTA:APPLICATION>
    </head>
    <frameset rows="60px, *">
        <frame src="topo.htm" name="topo" id="topo" application="yes" />
        <frame src="http://www.google.com" name="conteudo" id="conteudo" application="yes" />
    </frameset>
</html>
  • There's an HTA:APPLICATION tag that sets some properties to the file; 有一个HTA:APPLICATION标签,它为文件设置了一些属性; it's good to have, but it isn't a must. 它很好,但它不是必须的。
  • You NEED to place an application="yes" at the frames' tags. 需要在frames'标签处放置一个application="yes" 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). 它表示它们也属于该程序,并且应该可以访问所有数据(如果不这样,帧仍然会显示之前的错误)。

topo.htm topo.htm

<html>
    <head>
        <title>Topo</title>
        <script type="text/javascript">
            function copia_url() {
                campo.value = parent.conteudo.location;
            }
        </script>
    </head>
    <body style="background: lightBlue;" onload="copia_url()">
        <input type="button" value="Copiar URL" onclick="copia_url()" />
        <input type="text" size="120" id="campo" />
    </body>
</html>
  • You should notice that I didn't used any getElement function to fetch the field; 您应该注意到我没有使用任何getElement函数来获取字段; on HTA file, all elements that have an ID becomes instantly an object 在HTA文件中,具有ID的所有元素立即成为对象

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. 它解决了我的问题,看起来和你一样。

You can found more information here: http://www.irt.org/articles/js191/index.htm 您可以在此处找到更多信息: http//www.irt.org/articles/js191/index.htm

Enjoy =] 享受=]

You can use Ra-Ajax and have an iframe wrapped inside eg a Window control. 您可以使用Ra-Ajax并将iframe包裹在内部,例如Window控件。 Though in general terms I don't encourage people to use iframes (for anything ) 虽然总的来说我不鼓励人们使用iframe( 任何事情

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. 另一种方法是在服务器上加载HTML并将其作为Label或其他内容直接发送到Window中。 Check out how this Ajax RSS parser is loading the RSS items in the source which can be downloaded here (Open Source - LGPL) 看看这个Ajax RSS解析器如何加载源代码中的RSS项目,可以在这里下载(开源 - LGPL)

(Disclaimer; I work with Ra-Ajax...) (免责声明;我与Ra-Ajax合作...)

You can access the src property of the iframe but that will only give you the initially loaded URL. 您可以访问iframe的src属性,但这只会为您提供最初加载的URL。 If the user is navigating around in the iframe via you'll need to use an HTA to solve the security problem. 如果用户在iframe中导航,则需要使用HTA来解决安全问题。

http://msdn.microsoft.com/en-us/library/ms536474(VS.85).aspx http://msdn.microsoft.com/en-us/library/ms536474(VS.85).aspx

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. 查看链接,使用HTA并设置iframe的“application”属性将允许您访问document.href属性并解析所需的所有信息,包括DOM元素及其值(如果您愿意)。

Does this help? 这有帮助吗?

http://www.quirksmode.org/js/iframe.html http://www.quirksmode.org/js/iframe.html

I only tested this in firefox, but if you have something like this: 我只在firefox中测试过这个,但是如果你有这样的东西:

<iframe name='myframe' id='myframe' src='http://www.google.com'></iframe>

You can get its address by using: 您可以使用以下方式获取其地址:

document.getElementById('myframe').src

Not sure if I understood your question correctly but anyways :) 不确定我是否正确理解你的问题但反正:)

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. 好的,所以在这个应用程序中,有一个iframe,在其中为用户提供链接或一些容量,允许iframe浏览到某个外部站点。 You are then looking to capture the URL to which the user has browsed. 然后,您要捕获用户浏览过的URL。

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. 由于URL是外部源,因此您将通过javascript(或客户端访问)与此iframe交互的数量受限,这称为浏览器跨域安全性,因为显然您已发现。 There are clever work arounds, as presented here Cross-domain, cross-frame Javascript , although I do not think this work around applies in this case. 有巧妙的解决方法,如这里提出的跨域,跨框架Javascript ,虽然我不认为这种工作适用于这种情况。

About all you can access is the location, as you need. 您可以根据需要随时访问所有可访问的位置。

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. 尝试使用配置为显示javascript错误的IE或FF浏览网页。 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. 你会惊讶地发现有多少javascript错误被抛出,很大程度上是因为那里有很多容易出错的javascript,这只会继续激增。

This solution assumes that the iframe in question is the same "window" context where you are running the javascript. 此解决方案假定所讨论的iframe与运行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.) (意思是,它没有嵌入到另一个框架或iframe中,在这种情况下,javascript代码会更复杂,您可能需要递归搜索窗口层次结构。)

<iframe name='frmExternal' id='frmExternal' src='http://www.stackoverflow.com'></frame>
<input type='text' id='txtUrl' />
<input type='button' id='btnGetUrl' value='Get URL' onclick='GetIFrameUrl();' />

<script language='javascript' type='text/javascript'>
function GetIFrameUrl()
{
    if (!document.getElementById)
    {
        return;
    }

    var frm = document.getElementById("frmExternal");
    var txt = document.getElementById("txtUrl");

    if (frm == null || txt == null)
    {
        // not great user feedback but slightly better than obnoxious script errors
       alert("There was a problem with this page, please refresh.");
       return;
    }

    txt.value = frm.src;
}
</script>

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM