1

对于 ASP.NET、MVC 3、C# 网站,其主页具有指向其他页面的链接 -

在请求主页并将其加载到浏览器中之后,有没有办法可以“预加载”链接的页面,以便它们立即呈现而无需返回服务器?

示例 - 假设我正在销售 10 种产品。每个产品都会有自己的“产品详细信息”页面链接到主页。当有人进入首页时,我想在后台“预加载”一个或多个“产品详情”页面,所以当点击“产品详情”链接时,该页面将立即可用。

4

2 回答 2

1

你在这里想要什么有点奇怪。您始终可以通过 AJAX 加载这些页面,然后处理对适当超链接的点击,并用您拥有的内容替换整个 DOM,但值得付出努力和维护吗?也许PJAX会在这里提供帮助?

于 2012-11-15T14:48:31.653 回答
0

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. 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);
}
于 2012-11-15T15:30:02.350 回答