javascript 中的 iframe 位置

发布于 2024-09-07 01:25:03 字数 611 浏览 3 评论 0原文

在本例中,alert 为空; 我想读取 iFrame 的位置,为此我使用 object.src 但只有在设置其 src 属性时它才有效。但我使用 window.open 方法设置 iFrame 的位置,在这种情况下它不起作用。我做什么 ?

<body>    
<iframe id="location" name="address" style="width:700px; height:700px;">  
</iframe>    
<script type="text/javascript" language="javascript">  
    window.open("http://www.google.com","address");  
    function clickMe()  
    {  
        var src = document.getElementBy Id("location").src;  
        alert(src);  
    }  
</script>  
<input type="button" onclick="clickMe()" value="click Me"/>  
</body>

In this example, alert is blank;
I want to read the location of iFrame, I am using object.src for this but it work only if I set its src attribute. But I set iFrame's location using window.open method and in this case it is not working. What I do ?

<body>    
<iframe id="location" name="address" style="width:700px; height:700px;">  
</iframe>    
<script type="text/javascript" language="javascript">  
    window.open("http://www.google.com","address");  
    function clickMe()  
    {  
        var src = document.getElementBy Id("location").src;  
        alert(src);  
    }  
</script>  
<input type="button" onclick="clickMe()" value="click Me"/>  
</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅暮の光 2024-09-14 01:25:03

与其使用 window.open(),为什么不直接使用:

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

现在您可以使用 getElementById() 获取其位置

您可以通过访问 location.href 属性来做到这一点。当然,出于安全原因,如果 iframe 的 DOM 是另一个域,浏览器将不允许您访问它。

var oIframe = document.getElementById("location");
// for compatibility
var oDoc = oIframe.contentWindow || oIframe.contentDocument;
if (oDoc.document) {
    oDoc = oDoc.document;
}
alert(oDoc.location.href);

Instead of using window.open(), why don't you just use:

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

Now you can get its location using getElementById()

You can do that by accessing the location.href attribute. Of course, for security reasons, browsers won't let you access the iframe's DOM if it's another domain.

var oIframe = document.getElementById("location");
// for compatibility
var oDoc = oIframe.contentWindow || oIframe.contentDocument;
if (oDoc.document) {
    oDoc = oDoc.document;
}
alert(oDoc.location.href);
水中月 2024-09-14 01:25:03

如果您通过应在同一域中工作的句柄访问,则会出现相同的安全问题

<script>
var win;
function load(link) {
  win=window.open(link.href,link.target)
  return win?false:true;
}
function tell(winName) {  
  try {
    alert(win.location.href)
    try {
      var handle = window.open('',winName);
      alert(handle.location.href)
    }
    catch(e) {
      alert('oops getting location via window handle:'+e.message)
    }
  }
  catch(e) {
    alert('Ooops getting location:'+e.message)
  }
}  
</script>

<iframe src="about:blank" name="if1"></iframe><br/>
<a href="http://www.google.com" target="if1" onClick="return load(this)">Load foreign link</a><br />
<a href="showhref.html" target="if1" onClick="return load(this)">Load local link</a><br />
<a href="#" onClick="return tell('if1')">Tell</a><br />

Same security issue if you access via the handle which should work in the same domain

<script>
var win;
function load(link) {
  win=window.open(link.href,link.target)
  return win?false:true;
}
function tell(winName) {  
  try {
    alert(win.location.href)
    try {
      var handle = window.open('',winName);
      alert(handle.location.href)
    }
    catch(e) {
      alert('oops getting location via window handle:'+e.message)
    }
  }
  catch(e) {
    alert('Ooops getting location:'+e.message)
  }
}  
</script>

<iframe src="about:blank" name="if1"></iframe><br/>
<a href="http://www.google.com" target="if1" onClick="return load(this)">Load foreign link</a><br />
<a href="showhref.html" target="if1" onClick="return load(this)">Load local link</a><br />
<a href="#" onClick="return tell('if1')">Tell</a><br />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文