簡體   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