简体   繁体   English

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

[英]How to create a popup while a page is being loaded?

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. 加载页面后,将执行弹出窗口的代码(在.js文件中)。即使我在jsp文件的标题中调用js代码,在加载页面后仍会显示该代码。 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. 但是,如果在调用jsp之后在jsp文件中放置了一个“警报”脚本,则该脚本将在加载页面之前执行,而不是在弹出窗口之前执行。

The javascript code for the pop-up has this structure: 弹出窗口的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)
                }
            });
        }
    }
};

The code in the .jsp is something like this: .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

Any suggestion about a possible solution? 关于可能的解决方案有什么建议吗?

Javascript是从页面读取后执行的-在页面顶部放置<script>标记以创建加载div /对话框。

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(ie display:none in CSS) until the code is done executing. 如果只是代码需要在用户看到任何东西之前运行,则使页面主体不可见(即CSS中的display:none),直到代码执行完毕。 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. 也许这些都不是完美的解决方案,但是如果您尝试在服务器和客户端之间做某事,那么无论如何,可能会有一个更好的地方。

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

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