2012-09-06 164 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--------