javascript中的iframe位置
在本例中,警报为空; 我想读取iFrame的位置,为此我使用object.src,但只有在我设置了它的src属性时它才能工作。但我使用window.open方法设置了iFrame的位置,在本例中它不起作用。我做什么javascript中的iframe位置,javascript,dom,iframe,location,src,Javascript,Dom,Iframe,Location,Src,在本例中,警报为空; 我想读取iFrame的位置,为此我使用object.src,但只有在我设置了它的src属性时它才能工作。但我使用window.open方法设置了iFrame的位置,在本例中它不起作用。我做什么 <body> <iframe id="location" name="address" style="width:700px; height:700px;"> </iframe> <script type="text/ja
<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>
窗口打开(“http://www.google.com“,”地址“);
函数clickMe()
{
var src=document.getElementBy Id(“位置”).src;
警报(src);
}
与其使用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);
同样的安全问题,如果你通过句柄访问,它应该在同一个域中工作
<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 />
瓦尔温;
功能加载(链接){
win=window.open(link.href,link.target)
还赢?假:真;
}
函数tell(winName){
试一试{
警报(win.location.href)
试一试{
变量句柄=window.open(“”,winName);
警报(handle.location.href)
}
捕获(e){
警报(“oops通过窗口句柄获取位置:”+e.message)
}
}
捕获(e){
警报('Ooops获取位置:'+e.message)
}
}
我知道它,有问题的是,如果我设置了它的src属性,那么它就会工作,但我想知道当我们使用open方法时如何读取位置?哇!正在搜索如何访问该位置!许多其他人提到了src属性,但我没有设置该属性。在我的情况下,这是正确的解决方案!