2013-11-28 71 views
0

我想根據內容大小重新調整我的iframe的大小。經過大量的搜索,我發現了一種在url中使用哈希標籤來執行它的方法。 Here is the link to the reference document。我遇到類似TypeError: frameId is undefined的錯誤。我懷疑我的客人或原始頁面將frameId返回爲null。爲什麼會這樣呢?

我在本地主機上測試它。我使用兩個Yii站點進行測試。FrameId是undefined

這是我的框架視圖。

<?php 

Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/frame.js');?> 

<script type="text/javascript"> 

window.onload = function(event) { 
    window.setInterval(publishHeight, 300); 
} 


    </script> 
    <div> 
    <div><?php echo CHtml::button('Book Now', array('submit' => array('controller/action','param1'=>$param1,'param2'=>$param2))); ?></div> 

我的主頁是這樣

<?php 
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/FrameManager.js');?> 
<iframe src="http://localhost/mysite/controller/action?param1=7&param2=20" frameborder="0"scrolling="no" onload="FrameManager.registerFrame(this)"></iframe> 

這是我frame.js

function publishHeight() { 

if (window.location.hash.length == 0) return; 

var frameId = getFrameId(); 

if (frameId == '') return; 

var actualHeight = getBodyHeight(); 
var currentHeight = getViewPortHeight(); 

if (Math.abs(actualHeight - currentHeight) > 15) { 
    var hostUrl = window.location.hash.substring(1); 

    hostUrl += "#"; 
    hostUrl += 'frameId=' + frameId; 
    hostUrl += '&'; 
    hostUrl += 'height=' + actualHeight.toString(); 

    window.top.location = hostUrl; 
} 
} 

function getFrameId() { 

var qs = parseQueryString(window.location.href); 
var frameId = qs["frameId"]; 

var hashIndex = frameId.indexOf('#'); 

if (hashIndex > -1) { 
    frameId = frameId.substring(0, hashIndex); 
} 

return frameId; 

} 

function getBodyHeight() { 

var height, 
    scrollHeight, 
    offsetHeight; 

if (document.height) { 

    height = document.height; 

} else if (document.body) { 

    if (document.body.scrollHeight) { 
     height = scrollHeight = document.body.scrollHeight; 
    } 

    if (document.body.offsetHeight) { 
     height = offsetHeight = document.body.offsetHeight; 
    } 

    if (scrollHeight && offsetHeight) { 
     height = Math.max(scrollHeight, offsetHeight); 
    } 
    } 

return height; 
} 

function getViewPortHeight() { 

var height = 0; 

if (window.innerHeight) { 
    height = window.innerHeight - 18; 
} else if ((document.documentElement) && (document.documentElement.clientHeight)) { 
    height = document.documentElement.clientHeight; 
} else if ((document.body) && (document.body.clientHeight)) { 
    height = document.body.clientHeight; 
} 

return height; 

} 

function parseQueryString(url) { 

url = new String(url); 

var queryStringValues = new Object(), 
    querystring = url.substring((url.indexOf('?') + 1), url.length), 
    querystringSplit = querystring.split('&'); 

for (i = 0; i < querystringSplit.length; i++) { 
    var pair = querystringSplit[i].split('='), 
     name = pair[0], 
     value = pair[1]; 

    queryStringValues[name] = value; 
    } 

return queryStringValues; 

} 

這是我FrameManager.js

var FrameManager = { 

currentFrameId : '', 
currentFrameHeight : 0, 
lastFrameId : '', 
lastFrameHeight : 0, 
resizeTimerId : null, 

init: function() { 

    if (FrameManager.resizeTimerId == null) { 

     FrameManager.resizeTimerId = window.setInterval(FrameManager.resizeFrames, 500); 

    } 

}, 

resizeFrames: function() { 

    FrameManager.retrieveFrameIdAndHeight(); 

    if ((FrameManager.currentFrameId != FrameManager.lastFrameId) || (FrameManager.currentFrameHeight != FrameManager.lastFrameHeight)) { 

     var iframe = document.getElementById(FrameManager.currentFrameId.toString()); 

     if (iframe == null) return; 

     iframe.style.height = FrameManager.currentFrameHeight.toString() + "px"; 

     FrameManager.lastFrameId = FrameManager.currentFrameId; 
     FrameManager.lastFrameHeight = FrameManager.currentFrameHeight; 
     window.location.hash = ''; 

    } 

    }, 

    retrieveFrameIdAndHeight: function() { 

    if (window.location.hash.length == 0) return; 

    var hashValue = window.location.hash.substring(1); 

    if ((hashValue == null) || (hashValue.length == 0)) return; 

    var pairs = hashValue.split('&'); 

    if ((pairs != null) && (pairs.length > 0)) { 

     for(var i = 0; i < pairs.length; i++) { 

      var pair = pairs[i].split('='); 

      if ((pair != null) && (pair.length > 0)) { 

       if (pair[0] == 'frameId') { 

        if ((pair[1] != null) && (pair[1].length > 0)) { 

         FrameManager.currentFrameId = pair[1]; 
        } 
       } else if (pair[0] == 'height') { 

        var height = parseInt(pair[1]); 

        if (!isNaN(height)) { 

         FrameManager.currentFrameHeight = height; 
         FrameManager.currentFrameHeight += 15; 

        } 
       } 
      } 
     } 
    } 

}, 

registerFrame: function(frame) { 

    var currentLocation = location.href; 
    var hashIndex = currentLocation.indexOf('#'); 

    if (hashIndex > -1) { 

     currentLocation = currentLocation.substring(0, hashIndex); 
    } 

    frame.contentWindow.location = frame.src + '?frameId=' + frame.id + '#' + currentLocation; 

} 
}; 

window.setTimeout(FrameManager.init, 300); 

回答

0

我發現了問題。問題出在我的parseQueryString函數和我的主機url。實際上,爲了獲得frameId,我需要不同地查詢字符串,因爲與參考文獻中給出的url相比,我的url包含更多參數。所以我需要改變這樣的ParseQueryString函數

querystring = url.substring((url.indexOf('?') + 22), url.length), 

這將最終給我的frameId。真正的問題是我在java腳本中的知識不足。無論如何得到了答案,謝謝大家。