JavaScript技巧:让网页自动穿上外套

来源:岁月联盟 编辑:zhuzhu 时间:2007-12-05
 在框架网页中,通常使用src参数指定框架内的网页地址,我们要做的就是,当直接打开这个地址时,让它自动监测然后再穿上外套,当然,首先要在网页内添加检测代码,如下:

以下是引用片段:
<script> 
if(top.location==self.location) 

  top.location="index.htm?"+self.location; 

</script> 

  注意,index.htm是外套网页地址,接下来要做的就是,怎样让外套网页自动添加这部分内容,我们要在外套网页中解析网页地址,找到参数,然后将框架src参数指向该参数就行了,代码如下:

以下是引用片段:
<script> 
document.write('<iframe id="mid" name="mid" width="100%" height="100%" frameborder="0" scrolling="auto"') 
var n=self.location.href.indexOf("?")//查看是否包含参数 
if(n>0)//存在参数 

//指向参数 
document.write(" src="+self.location.href.substr(n+1)) 

document.write('></iframe>') 
</script>