2012-09-06 170 views
1

比如我在index.html的一些鏈接:iframe src更改

<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?

+2

如果可以的話,這樣做在服務器端 – Alexander

+0

@Defense:見我的回答如下一個簡單的解決方案。 –

+0

@Alexander and Defense:在我的回答中提到的解決方案如何? –

回答

2

一個簡單的解決方案:

您在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); 
}); 

希望這將是你的答案。謝謝!

SEE DEMO

參見網頁:

+0

謝謝,這對我來說是工作。 – Defense

0

認爲你有ID = iframeid一個iframe,然後

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

在查詢字符串發送您的目標頁面這樣

$('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, " ")); 
} 
0

如果你真的想在你的客戶端做重定向..

在你message.html文件,把這個:

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

然後在添加iframe.html

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

沒有測試它,但你說的步驟可能會導致和重定向的無限循環。

0

你可以改變你的鏈接標籤這樣

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

,並在iframe中。html頁面的onload載體作用寫這個劇本

//---------------- 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--------