日期:2014-05-17  浏览次数:20647 次

js中获取ifrme子页面值的执行顺序问题
父页面代码如下  
js代码:
function  pTest(){
var url = "child.jsp";
 $("#mainFrame").attr("src",url);

  var msg = document.mainFrame.resultStr;//这个是获得子页面的一个js字符串变量
}
html:
<div class="right_divbottom">
<iframe frameborder='0' id="mainFrame" name="mainFrame" src="" style="overflow: hidden;" width='100%' height="100%" ></iframe>
 </div>

子页面如下:

body有个
initDeptData(){
   $.ajax( {  
          type : "POST",  
          url : url,  
          data: "",  //整个表单提交
          success : function(msg) { 
          
//这里面经过业务处理返回一个字符串
resultStr = msg;
 
          }  ,
          error: function(){                      
              alert('初始化部门信息出错!'); 
          }
      });
}

问题如下,我现在要在父页面中获得document.mainFrame.resultStr,但是有个问题,因为子页面initDeptData()这个方法会消耗一定时间,我现在想要当resultStr = msg;返回这个msg的时候立刻去获取document.mainFrame.resultStr,怎么做???
我现在是用setTimeout,但是这样不保险,也可能造成时间的浪费

求解了....




------解决方案--------------------
您可以在子页面调用父页面的js方法,然后把这个字符串传递过去。应该可以的。。。
------解决方案--------------------
在子页面为父页面变量赋值
------解决方案--------------------
比如你子页面定义
<script>
var zipphone = "<?= $phone ?>";
 </script>


你可以在父页面定义这样一个方法取值:
function zipPhoneCallback(zipphone) {
    ((console&&console.log)
------解决方案--------------------
alert)("zipphone = " + zipphone);
}


然后子页面这样传值:
window.parent.zipPhoneCallback(zipphone);