简体   繁体   English

ASP.NET,MVC 3,C#网站,预加载链接的页面

[英]ASP.NET, MVC 3, C# website, pre-load linked pages

For an ASP.NET, MVC 3, C# website with a home page that has links to other pages - 对于ASP.NET,MVC 3,C#网站,其主页具有指向其他页面的链接-

After the home page has been requested and loaded into the browser, is there a way that the linked pages can be "pre-loaded" so they will rendered immediately without a trip back to the server? 在请求主页并将其加载到浏览器之后,是否可以通过某种方式“预加载”链接的页面,以便它们可以立即呈现而无需返回服务器?

EXAMPLE - Let's say I was selling 10 products. 示例-假设我卖了10种产品。 Each product will have their own "product detail" page linked from the home page. 每个产品都有一个从首页链接的自己的“产品详细信息”页面。 When someone goes to the home page, I want to "pre-load" one or more of the "product detail" pages in the background, so when the "product detail" link is clicked, the page will be immediately available. 当某人转到主页时,我想在后台“预加载”一个或多个“产品详细信息”页面,因此,当单击“产品详细信息”链接时,该页面将立即可用。

That's kinda strange what you want here. 您在这里想要的东西有点奇怪。 You can always load these pages via AJAX, then handle clicks on appropriate hyperlinks and replace entire DOM with what you have, but is it worth the effort and maintenance? 您总是可以通过AJAX加载这些页面,然后处理对相应超链接的点击,然后用您拥有的内容替换整个DOM,但是值得付出努力和维护吗? Maybe PJAX will help here? 也许PJAX在这里会有所帮助?

If you really want to do some sort of parallel loading, you can have an element that is not actual displayed on the page but that holds valid content. 如果您确实想进行某种并行加载,则可以在页面上不实际显示一个元素,但该元素可以容纳有效内容。 Something like: 就像是:

<div id="parallel_load" style="position:absolute;left: -1000px;width: 100px;></div>

Then you can create a queue of things you want to go get. 然后,您可以创建要排队的东西队列。 Since it's almost exclusively going to be images that you'll want to preload, you can simply have a list that requests one image at a time from a service, creates a new element and inserts it into this div. 由于几乎完全是要预加载的图像,因此您可以简单地得到一个列表,该列表一次从服务请求一个图像,创建一个新元素并将其插入到该div中。 That would force the browser to load the new image in the background while the user is doing other things ... clicking, logging in. 这将迫使浏览器在用户执行其他操作(例如单击并登录)时在后台加载新图像。

$(LoadNextImage(0));

function LoadNextImage(index)
{
    // do ajax call here to get the url to the image and id
    // use index to keep track of what images have loaded maybe

    var image = $('<img id="' + id + '"');
    image.attr('src', urlFromAjax);
    image.appendTo('#parallel_load');

    // moreImages would be some response from the server that
    // says you're not done yet possibly
    if (moreImages) 
        setTimeout(LoadNextImage, 500);
}

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

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