繁体   English   中英

改变iframe src

[英]Change iframe src

例如,我在index.html中有一些链接:

<a href="message.html">Go to message page</a>

在其他页面中,iframe.html我有一个iframe:

<iframe id="iframe" src="profile.html" frameborder="0" scrolling="no"></iframe>

是否可以这样做,当我点击“转到消息页面”链接时,它会将我重定向到iframe.html并将iframe src更改为message.html?

简单的解决方案:

index.html的链接应如下所示:

<a href="iframe.html?message.html">Go to message page</a>

iframe.html执行以下操作:

$(function () {
    var url = window.location.href;
    var QueryStr = url.split('?')[1];    
    $('#iframe').attr('src', QueryStr);
});

希望这将是你的答案。 谢谢!

看看演示

见页:

像这样在查询字符串中发送您的目标页面

  $('a[href="message.html"]').click(function (e) {
        e.preventDefault()
        window.location = 'iframe.html?src="message.html"';
  })

在iframe.html上执行此操作

$(function () {
    $('#iframe').attr('src', getParameterByName('src'));
});

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

假设您有一个id = iframeid的iframe,那么

document.getElementById("iframeid").src = "new/address/here";

如果你真的想在你的客户端进行重定向..

在你的message.html文件中,输入:

<script type="text/javascript">
 window.location = "iframe.html"
</script>

然后在iframe.html上添加:

document.getElementById("iframe").src = "message.html";

没有测试过,但是你说的步骤可能会导致重定向无限循环。

你可以改变这样的链接标签

<a href="iframe.html?ifsrc=message.html">Go to message page</a> 

并在iframe.html page onload functin中编写此脚本

//---------------- within head tag----------of iframe.html--------
    function GET() {
       var data = [];
        for(x = 0; x < arguments.length; ++x)
                data.push(location.href.match(new RegExp("/\?".concat(arguments[x],"=","([^\n&]*)")))[1])
                return data;
        }

        //--- on iframe.html onload function
    function iframeOnload(){

    document.getElementById("iframe").src =  GET("id")[0];

    }

//---------------- within head tag----------of iframe.html--------

暂无
暂无

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

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