使用jquery缓存对iframe中元素的引用

时间:2010-09-03 18:14:57

标签: javascript jquery jquery-ui iframe jquery-dialog

我正在使用一个jquery对话框,它使用iframe让用户知道会话即将到期。我想用倒计时每秒更新一次这个对话框,因此我想缓存对倒计时元素的引用,这样我们就不必每秒查询一次DOM了。

我尝试过这么多种不同的方法,但似乎无法让它发挥作用。

$(documnet).ready(function() {
   $("<iframe src='../active_timer.html' id='iframeDialog' />").dialog({
       autoOpen: false,
       title: "Your session is about to expire!",
       modal: true,
       width: 400,
       height: 200,
       closeOnEscape: false,
       draggable: false,
       resizable: false,
       buttons: {
           "Yes, Keep Working": function(){
               $(this).dialog("close");
           },
           "No, Logoff": function(){
               //log user out
           }
       }
    });

    // cache a reference to the countdown element.
    // these below don't cache the reference I have tried them all.
    var $countdown = $("#iframeDialog").contents().find("#dialog-countdown");
    var $countdown = $("#dialog-countdown", window.child);
    var $countdown = $("#dialog-countdown", $('iframe').get(0).contentDocument);

    //this is where I update the #dialog-countdown every second after the reference is made.
});

active_timer.html:

<div id="dialog" style="overflow:hidden;">
    <p>
        <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 25px 0;"></span>
        You will be logged off in <span id="dialog-countdown"></span> seconds.
    </p>
    <p>Do you want to continue your session?</p>
</div>

1 个答案:

答案 0 :(得分:0)

加载时,active_timer.html应将active_timer.html中DOM元素的引用传递给其父元素。

function onLoad() {
   window.opener.childCountdown = document.getElementById("dialog-countdown");
}

在父母:

var $countdown = $(childCountdown);