上次说了iframe自动适应代码,但发现一个问题,就是浏览器的url永远没变,这个在将链接拷贝给其他人时就不方便了。
如果使用PHP模板方式,又太复杂,最好想了个笨办法来实现,即在URL中添加"#字符识别“方式来实现。
下面问index全部代码,html部分实现页面居中,JSP部分实现自适应高度和根据url #字符后的信息加载页面
<html>
<head>
<style>
.page {
width: 800px;
}
.left {
width: 200px;
float: left;
}
.right {
width: 750px;
float: left;
}
</style>
</head>
<body>
<center>
<div class="page" align="center">
<div class="top">
<iframe align="center" valign="middle" id="top" name="top" marginwidth="0" marginheight="0" width="100%" height="225" src="top.htm" scrolling="no" frameborder="0"></iframe>
</div>
<div class="middle">
<iframe width="100%" align="center" height="100" id="main" name="main" onload="Javascript:SetWinHeight(this)" frameborder="0" scrolling="no" src="main.htm"></iframe>
</div>
<div class="bottom">
<iframe id="bottom" name="bottom" marginwidth="0" marginheight="0" width="100%" height="20" scrolling="no" src="copyright.htm" frameborder="0"></iframe>
</div>
</div>
</center>
</body>
</html>
<script language="javascript">
function SetWinHeight(obj)
{
var win=obj;
obj.height=200;
if (document.getElementById)
{
if (win && !window.opera)
{
if (win.contentDocument && win.contentDocument.body.offsetHeight)
{
win.height = win.contentDocument.body.offsetHeight;
}
else if(win.Document && win.Document.body.scrollHeight)
{
win.height = win.Document.body.scrollHeight;
}
}
}
}
function GetArgsFromHref()
{
var uri = parent.window.location.hash;
var args = uri.split("#");
if(args[0] == uri) /*参数为空*/
{
return "main";
}
return args[1];
}
function Load_page()
{
document.getElementById("main").src = GetArgsFromHref() + ".htm";
}
Load_page();
</script>
在需要显示到main的frame中的每个页面均加入如下代码(这个比较繁琐,但幸好代码不变): <script language="javascript">function GetArgsFromHref(){var uri = parent.window.location.hash;var args = uri.split("#");if(args[0] == uri) /*参数为空*/{return "main";} return args[1];}
function GetFileName(){var uri = window.location.href;var args = uri.split("/");var sn = args.length;if(sn != 0){sn = sn - 1;}var names = args[sn].split(".");return names[0];}
function set_url(){if(GetArgsFromHref() != GetFileName()){parent.window.location.hash = GetFileName();}}set_url();
</script>