简体   繁体   English

document.write 正在杀死页面

[英]document.write is killing page

I created a timer and did not notice the document.write was killing the page because everything was working great.我创建了一个计时器并且没有注意到 document.write 正在杀死页面,因为一切正常。 However, when the timer goes to 0, it only displays what is in the document.write function rather than everything else in my page.但是,当计时器变为 0 时,它只显示 document.write 函数中的内容,而不是我页面中的其他所有内容。

This is what is killing the page and only the text in ()'s is what is showing..这就是杀死页面的原因,只有 () 中的文本显示..

document.write("The NFL Season is here!!");

How can I make this so that the timer would stop and this text displays or this text can display on my page without killing the rest of the code?我怎样才能做到这一点,以便计时器停止并显示此文本或此文本可以显示在我的页面上而不会终止其余代码?

Here is my full code to demonstrate how I am doing this.这是我的完整代码来演示我是如何做到这一点的。

<script type="text/javascript">
        function cdtd () {
            var nflSeason = new Date ("September 10, 2015 08:30:00");
            var now = new Date();
            var timeDiff = nflSeason.getTime() - now.getTime();
            if (timeDiff < 0) {
                clearTimeout(timer);
                document.write("The NFL Season is here!!");
                //Run any code needed for countdown completion here.
            }

            var seconds = Math.floor(timeDiff / 1000);
            var minutes = Math.floor(seconds / 60);
            var hours = Math.floor(minutes / 60);
            var days = Math.floor(hours / 24);
            hours %= 24;
            minutes %= 60;
            seconds %= 60;

            document.getElementById("daysBox").innerHTML = days;
            document.getElementById("hoursBox").innerHTML = hours;
            document.getElementById("minsBox").innerHTML = minutes;
            document.getElementById("secsBox").innerHTML = seconds;

            var timer = setTimeout('cdtd()',1000);
        }
</script>

HTML HTML

<div class="countdown_out">
<div id="countdown_title">NFL Season Opener Countdown</div>
<div class="countdown_position">
    <div class="countdownBox">
        <div class="countdown_time_category">Days</div>
        <div id="daysBox" class="countdown_time"></div>
    </div>
    <div class="countdownBox">
        <div class="countdown_time_category">Hours</div>
        <div id="hoursBox" class="countdown_time"></div>
    </div>
    <div class="countdownBox">
        <div class="countdown_time_category">Minutes</div>
        <div id="minsBox" class="countdown_time"></div>
    </div>
    <div class="countdownBox">
        <div class="countdown_time_category">Seconds</div>
        <div id="secsBox" class="countdown_time"></div>
    </div>
</div>

as everybody suggested.正如大家所建议的那样。 document.write will clear everything from the DOM . document.write将清除DOM所有内容。 the best way to write this would be to have a DIV in your HTML and set that div in your javascript code.编写此代码的最佳方法是在您的 HTML 中包含一个DIV ,并在您的 javascript 代码中设置该 div。

here's what your HTML should look like这是你的HTML应该是什么样子

<div id="page_message" style="display: none;"></div>
<div class="countdown_out">
<div id="countdown_title">NFL Season Opener Countdown</div>
<div class="countdown_position">
    <div class="countdownBox">
        <div class="countdown_time_category">Days</div>
        <div id="daysBox" class="countdown_time"></div>
    </div>
    <div class="countdownBox">
        <div class="countdown_time_category">Hours</div>
        <div id="hoursBox" class="countdown_time"></div>
    </div>
    <div class="countdownBox">
        <div class="countdown_time_category">Minutes</div>
        <div id="minsBox" class="countdown_time"></div>
    </div>
    <div class="countdownBox">
        <div class="countdown_time_category">Seconds</div>
        <div id="secsBox" class="countdown_time"></div>
    </div>
</div>

and update your Javascript code to look like this.并将您的Javascript代码更新为如下所示。

<script type="text/javascript">
    function cdtd() {
        var nflSeason = new Date("September 10, 2015 08:30:00");
        var now = new Date();
        var timeDiff = nflSeason.getTime() - now.getTime();
        if (timeDiff < 0) {
            clearTimeout(timer);
            document.getElementById("page_message").innerHTML = 'The NFL Season is here!!';
            document.getElementById("page_message").style.display = 'inline';

            //Run any code needed for countdown completion here.
        }

        var seconds = Math.floor(timeDiff / 1000);
        var minutes = Math.floor(seconds / 60);
        var hours = Math.floor(minutes / 60);
        var days = Math.floor(hours / 24);
        hours %= 24;
        minutes %= 60;
        seconds %= 60;

        document.getElementById("daysBox").innerHTML = days;
        document.getElementById("hoursBox").innerHTML = hours;
        document.getElementById("minsBox").innerHTML = minutes;
        document.getElementById("secsBox").innerHTML = seconds;

        var timer = setTimeout('cdtd()', 1000);
    }
</script>

Hope this helps.希望这可以帮助。

You should use document.write only from synchronious code before page is fully loaded.在页面完全加载之前,您应该只从同步代码中使用document.write Do not use it from timers, event handlers and scripts marked by defer or async .不要在计时器、事件处理程序和由deferasync标记的脚本中使用它。 You have to use other way of changing DOM.您必须使用其他方式来更改 DOM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM