【问题标题】:html pages auto cicling using js scripthtml页面使用js脚本自动循环
【发布时间】:2018-02-16 11:22:56
【问题描述】:

我有一个打印 html 页面的 servlet。我希望这些页面使用 js 脚本自动循环。所以我加载了一个 html 页面,3 秒后它刷新,然后我加载了另一个 html 页面(由同一个 servlet 打印)。

在 servlet 中: - 阅读我存储正在打印的页面索引的会话; - 基于这个索引,我打印页面并更新索引(如果我打印最后一页,它会增加或返回到 0) - 将索引存储在 div 元素中,这样我就可以通过 javascript 函数找到它并使用 js 函数刷新 html 页面

    out.println("<div id=\"storedDiv\">");
    int indexRefresh= (int) request.getSession().getAttribute("indexRefresh");
    out.println(Integer.toString(indexRefresh) );
    indexRefresh=indexRefresh+1;
    if (indexRefresh==Tot) {
        indexRefresh=0;
    }
    request.getSession().setAttribute("indexRefresh", indexRefresh);
    out.println("</div>");

js函数在html页面中找到div元素并更新页面

function refresh(){
    var element = document.getElementById("storeDiv");
    if (element==0){
    setTimeout(function() {
          window.location.href = "http://localhost:9080/HydroGui/Query?anno=2018&id=1";
        }, 3000);   
    }
    if (element==1){
        setTimeout(function() {
              window.location.href = "http://localhost:9080/HydroGui/Query?anno=2018&id=2";
            }, 3000);   
        }
}

但是我在哪里调用函数呢?如果我使用

window.onload = refresh();

在我将索引存储在 div 中之前加载页面。 那么最好的方法是什么?

【问题讨论】:

    标签: javascript html refresh


    【解决方案1】:

    使用innerHTML

    var element = document.getElementById("storeDiv").innerHTML;
    if (element == '0') {
        //...
    }
    

    【讨论】:

    • 在页面加载之前我看不到 div 元素。
    • @Hydronit,如果脚本包含在 div 之后,您可以看到 div 元素,只需调用 refresh 函数
    猜你喜欢
    • 2012-04-02
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2012-08-16
    • 2011-10-12
    • 2020-01-29
    相关资源
    最近更新 更多