如何在页面加载时创建弹出窗口?

发布于 2024-12-08 23:04:48 字数 1478 浏览 0 评论 0原文

过去两天我一直在研究这个话题。我需要在页面加载时显示弹出窗口,但只有在页面加载后才显示它。

弹出窗口的代码(在 .js 文件中)在页面加载后执行 即使我调用jsp文件头中的js代码,页面加载后仍然显示。但是,如果我在调用 jsp 之后在 jsp 文件中放入“警报”脚本,则该脚本会在页面加载之前执行,但不会在弹出窗口之前执行。

弹出窗口的 javascript 代码具有以下结构:


package.popup = {
        init : function() {
        this._mypopup();
    },

    _myPopup : function() {
        //...(code)
            alert("This alert is also displayed after the page is loaded");
            jQ.get('myPopupData.do?ajax=ajax', function(popupData) {
        //...(code)
                }
            });
        }
    }
};

.jsp 中的代码如下所示:

//... some code
<div class="introSection">
    <div class="header">
        <header>
            <hdgroup>
                <h1>Welcome to myPage.</h1>
            </hdgroup>       
            <jwr:script src="/javascript/bundles/jquery-1.6.1.min.js" useRandomParam="false" />
            <jwr:script src="/javascript/bundles/homePage.js" useRandomParam="false" />
            <jwr:script src="/javascript/bundles/mobilePopup.js" useRandomParam="false" />     
            <script language="javascript">alert("This alert is placed after the popup code is called in the header, but it's showed before");</script>         
        </header>
    </div>
    <ul>
        //more code
    </ul>          
//more code

关于可能的解决方案有什么建议吗?

I've spent the last two days doing research about this topic. I need a popup to be displayed while a page is loading, but I only get it to display once the page is loaded.

The code for the pop-up (in a .js file) is executed after the page is loaded
Even if I call the js code in the header of the jsp file, it is still displayed after the page is loaded. However if I put an "alert" script in the jsp file after the call to the jsp, it is executed before the page is loaded, but not the popup.

The javascript code for the pop-up has this structure:


package.popup = {
        init : function() {
        this._mypopup();
    },

    _myPopup : function() {
        //...(code)
            alert("This alert is also displayed after the page is loaded");
            jQ.get('myPopupData.do?ajax=ajax', function(popupData) {
        //...(code)
                }
            });
        }
    }
};

The code in the .jsp is something like this:

//... some code
<div class="introSection">
    <div class="header">
        <header>
            <hdgroup>
                <h1>Welcome to myPage.</h1>
            </hdgroup>       
            <jwr:script src="/javascript/bundles/jquery-1.6.1.min.js" useRandomParam="false" />
            <jwr:script src="/javascript/bundles/homePage.js" useRandomParam="false" />
            <jwr:script src="/javascript/bundles/mobilePopup.js" useRandomParam="false" />     
            <script language="javascript">alert("This alert is placed after the popup code is called in the header, but it's showed before");</script>         
        </header>
    </div>
    <ul>
        //more code
    </ul>          
//more code

Any suggestion about a possible solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最单纯的乌龟 2024-12-15 23:04:48

Javascript 在从页面读取时执行 - 在页面顶部放置一个

Javascript is executed as its read from the page - put a <script> tag at the top of the page to create the loading div / dialog.

红ご颜醉 2024-12-15 23:04:48

我不确定在页面加载之前您想要做什么,但如果它与不希望用户在决策点之前下载整个页面有关,我只会有一个中间页面。

如果只是需要在用户看到任何内容之前运行的代码,请使页面主体不可见(即 CSS 中的 display:none),直到代码执行完毕。代码完成后,使主体再次可见。

也许这些不是优雅的解决方案,但是如果您尝试在服务器和客户端之间执行某些操作,那么无论如何可能有更好的地方可以执行此操作。

I'm not sure what it is you're trying to do before the page loads, but if it has to do with not wanting the user to download the whole page before a decision point, I would just have an intermediate page instead.

If it is just code that needs to run before the user sees anything, make the body of the page invisible(i.e. display:none in CSS) until the code is done executing. After the code has finished, make the body visible again.

Perhaps these aren't elegant solutions, but if you're trying to do something between the server and the client, there's probably a better place to be doing it anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文