简体   繁体   English

jQuery对页面加载的影响

[英]JQuery effect on page load

I have a page that does some time-consuming stuff in the page load(actually that is all it does). 我有一个页面在页面加载中做了一些耗时的工作(实际上就是它所做的全部)。 I would like to present the users with a decent message and then replace it once the process is complete with the actual results. 我想向用户展示一个不错的消息,然后在实际结果完成后将其替换。

Every thing I try make the UI to render after the long process ends, which doesn't help. 我尝试做的所有事情都会使UI在漫长的过程结束后呈现,这无济于事。 I tried using an UpdatePanel to make the page render and then trigger the update from the client but it doesn't work either. 我尝试使用UpdatePanel使页面呈现,然后从客户端触发更新,但是它也不起作用。

Can someone give me a clue on how to do this? 有人可以告诉我如何执行此操作吗?

Thanks 谢谢

I'd suggest you do the following: 我建议您执行以下操作:

  1. Create a new Page that only shows whatever it is you want to show while it is loading. 创建一个新页面,该页面仅显示加载时要显示的内容。 A loading graphic or otherwise. 正在加载图形或其他方式。
  2. Via AJAX call your current page and have it return success or failure or whatever you want to return to page 1. 通过AJAX调用您的当前页面,让它返回successfailure或您想要返回到页面1的任何内容。
  3. After the ajax call returns you can replace the loading icon or html with your sucess graphics. 在ajax调用返回之后,您可以用成功的图形替换正在加载的图标或html。

HTML page 1: <div id="content"><img src="images/loading.png" alt="loading" /></div> HTML页面1: <div id="content"><img src="images/loading.png" alt="loading" /></div>

jQuery: jQuery的:

$.ajax({
    type: "GET",
    timeout: 30000,  // set accordingly
    url: "/some/url", // this is the page that will do all the work
    beforeSend: function() {
      // do some stuff
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
      alert("An error has occurred making the request: " + errorThrown); // if there was an issue
    }, 
    success: function() {
    $('#content').html('Success!!'); // change icon or what you need to do.
    }
    });

I can't get the UI to format the code correctly. 我无法获得用于正确设置代码格式的UI。 Jquery ajax reference: http://api.jquery.com/jQuery.ajax/ jQuery Ajax参考: http//api.jquery.com/jQuery.ajax/

You could use the jQuery BlockUI plugin - in your ready handler , you'd block, process, then unblock. 您可以使用jQuery BlockUI插件 -在就绪的处理程序中 ,进行阻止,处理然后取消阻止。

If there's a large amount of data flowing down to the client, there will be a perhaps uncomfortable period before the ready handler kicks off and blocks the page, so another strategy is to have a <div> that covers the page (with your "please wait, loading" message in it) that you hide at the end of your ready handler. 如果有大量数据流到客户端,则就绪处理程序启动并阻塞页面之前,可能会有一段不舒服的时间,因此另一种策略是使<div>覆盖页面(请使用“等待,正在加载其中的消息),您将其隐藏在就绪处理程序的末尾。

If you are using ASP.NET WebForms you can use UpdatePanel and UpdateProgress to display image while something is loading. 如果使用的是ASP.NET WebForms,则可以在加载某些内容时使用UpdatePanel和UpdateProgress来显示图像。 Just point updateprogress control to updatepanel control. 只需将updateprogress控件指向updatepanel控件即可。 UpdateProgress will containg loading image and updatepanel you main content and controls. UpdateProgress将包含加载图像和updatepanel您的主要内容和控件。 However, UpdateProgress and UpdatePanel are an ugly tools written by Microsoft, I would strongly recomment using jQuery with its true AJAX calls (it will involve more work though) 但是,UpdateProgress和UpdatePanel是Microsoft编写的丑陋工具,我强烈建议使用jQuery及其真正的AJAX调用(尽管这样做会涉及更多工作)

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

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