场景:主页面 (emo_windowname.html)一个IFrame(framePreview),在页面加载或者单击按钮时,让IFrame加载一个页面(http://...../正文.html).

   条件:正文.html 预告加入脚本,或者使用HttpModule加入脚本:

$(function(){
    window.name = document.body.scrollHeight;
    
});

  这样,在主页面中,就可以通过window.name把高度取过来,然后设置一下 framePreview的高度。

  代码如下:

  主页面代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>跨域IFrame自动高度</title>
<script language="javascript" src="../Library/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">

    function autoHeightIFrameNavigate(iframeId,url)
    {
        var iframe = $('#' + iframeId);
        iframe.attr('_name_',iframe.attr('name'))    //备份原来的 name 
        .attr('src',url)                            //设置URL
        .one('load',function () {
            this.contentWindow.location = "about:blank";    //导向代理页面,我直接使用了这个
            $(this).one('load',function () {
                var msg = this.contentWindow.name;            //得到值 这个值就是高度了
                this.contentWindow.name = $(this).attr('_name_');    //恢复原来的 Name
                this.contentWindow.location = url;            //再次导向目标页面
                
                try
                {
                    var height = eval(msg);                    //得到并设置高度
                    alert(height);
                    iframe.css('height', height + 'px');
                
                }
                catch(e)
                {
                    alert('目标页面没有设置高度到 window.name')
                }
                
            })
        
        
        })
    }
    $(function () {
        $('#btnNavigator').click(function(){
            autoHeightIFrameNavigate('iframePreview', $('#tbUrl').val());
        });
    
    })

</script>

</head>
<body>
    <input type="text" ></iframe>
    </div>


</body>
</html>


示例目标页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    <title>Untitled</title>
    <script type="text/javascript" src="/js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
    $(function(){
        window.name = document.body.scrollHeight;
    
    });
    </script>

</head>

<body>

</body>
</html>

 

  出处: http://evlon.cnblogs.com

相关文章:

  • 2021-05-19
  • 2021-05-20
  • 2021-12-12
  • 2022-12-23
  • 2022-02-02
猜你喜欢
  • 2022-01-06
  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案