Javascript FrameId未定义
我正在尝试根据内容大小重新调整iframe的大小。在谷歌搜索了很多之后,我发现了一种在url中使用哈希标记的方法。。我得到了一个类似于Javascript FrameId未定义,javascript,html,css,iframe,yii,Javascript,Html,Css,Iframe,Yii,我正在尝试根据内容大小重新调整iframe的大小。在谷歌搜索了很多之后,我发现了一种在url中使用哈希标记的方法。。我得到了一个类似于TypeError:frameId未定义的错误。我怀疑我的来宾或原始页面将frameId返回为null。为什么会这样 我正在本地主机上测试它。我使用两个Yii站点来测试这个 这是我的观点 <?php Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/j
TypeError:frameId未定义的错误。我怀疑我的来宾或原始页面将frameId返回为null。为什么会这样
我正在本地主机上测试它。我使用两个Yii站点来测试这个
这是我的观点
<?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>
window.onload=函数(事件){
窗口设置间隔(publishHeight,300);
}
我的主页是这样的
<?php
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/FrameManager.js');?>
<iframe src="http://localhost/mysite/controller/action?param1=7¶m2=20" frameborder="0"scrolling="no" onload="FrameManager.registerFrame(this)"></iframe>
querystring = url.substring((url.indexOf('?') + 22), url.length),
这是我的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;
}
函数publishHeight(){
if(window.location.hash.length==0)返回;
var frameId=getFrameId();
if(frameId='')返回;
var actualHeight=getBodyHeight();
var currentHeight=getViewPortHeight();
如果(数学abs(实际高度-当前高度)>15){
var hostUrl=window.location.hash.substring(1);
hostUrl+=“#”;
hostUrl+='frameId='+frameId;
hostUrl+='&';
hostUrl+='height='+actualHeight.toString();
window.top.location=主机URL;
}
}
函数getFrameId(){
var qs=parseQueryString(window.location.href);
var frameId=qs[“frameId”];
var hashIndex=frameId.indexOf('#');
如果(hashIndex>-1){
frameId=frameId.substring(0,hashIndex);
}
返回frameId;
}
函数getBodyHeight(){
变量高度,
卷轴高度,
远视;
if(文件高度){
高度=文件高度;
}else if(document.body){
if(document.body.scrollHeight){
高度=滚动高度=document.body.scrollHeight;
}
if(document.body.offsetHeight){
高度=偏移视线=document.body.offsetHeight;
}
如果(滚动高度和离视(&F){
高度=数学最大值(滚动高度,离视);
}
}
返回高度;
}
函数getViewPortHeight(){
var高度=0;
if(窗内高度){
高度=窗内高度-18;
}else if((document.documentElement)和&(document.documentElement.clientHeight)){
高度=document.documentElement.clientHeight;
}else if((document.body)和&(document.body.clientHeight)){
高度=document.body.clientHeight;
}
返回高度;
}
函数parseQueryString(url){
url=新字符串(url);
var queryStringValues=新对象(),
querystring=url.substring((url.indexOf(“?”)+1),url.length),
querystringSplit=querystring.split('&');
对于(i=0;i
这是我的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);
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)返回;
iframe.style.height=FrameManager.currentFrameHeight.toString()+“px”;
FrameManager.lastFrameId=FrameManager.currentFrameId;
FrameManager.lastFrameHeight=FrameManager.currentFrameHeight;
window.location.hash='';
}
},
RetrieveFrameId和高度:函数(){
if(window.location.hash.length==0)返回;
var hashValue=window.location.hash.substring(1);
if((hashValue==null)| |(hashValue.length==0))返回;
var pairs=hashValue.split('&');
如果((pairs!=null)&&(pairs.length>0)){
对于(变量i=0;i0)){
if(对[0]==“frameId”){
if((对[1]!=null)和&(对[1]。长度>0)){
FrameManager.currentFrameId=对[1];
}
}else if(对[0]=“高度”){
变量高度=parseInt(对[1]);
如果(!isNaN(高度)){
FrameManager.currentFrameHeight=高度;
FrameManager.currentFrameHeight+=15;
}
}
}
}
}
},
registerFrame:函数(帧){
var currentLocation=location.href;
var hashIndex=currentLocation.indexOf('#');
如果(hashIndex>-1){
currentLocation=currentLocation.substring(0,hashIndex);
}
frame.contentWindow.location=frame.src+'?frameId='+frame.id+'#'+currentLocation;
}
};
setTimeout(FrameManager.init,300);
我发现了这个问题。问题在于我的parseQueryString函数和主机url。实际上,为了获得frameId,我需要以不同的方式查询字符串,因为我的url与参考中给出的url相比包含更多的参数。所以我需要像这样更改ParseQueryString函数
<?php
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/FrameManager.js');?>
<iframe src="http://localhost/mysite/controller/action?param1=7¶m2=20" frameborder="0"scrolling="no" onload="FrameManager.registerFrame(this)"></iframe>
querystring = url.substring((url.indexOf('?') + 22), url.length),
最终会给我的frameId。真正的问题是我对java脚本的了解不够。无论如何,我得到了答案。谢谢大家