简体   繁体   English

悬停时预加载页面,点击时链接

[英]Preload page on hover and link on click

I'm playing around with a possibility to preload a page using ajax on hover over a button and have this page available quicker on click on this button. 我正在尝试在鼠标悬停在按钮上时使用Ajax预加载页面,并在单击该按钮时可以更快地访问该页面。 I'm quite at the beginning and can't really find a good solution for this / might be totally wrong. 我还处于起步阶段,因此无法真正找到一个好的解决方案,这可能是完全错误的。 Here is what I have tried, looks like a little promissing for me: 这是我尝试过的方法,对我来说似乎有些许:

document.getElementById('link').addEventListener('mouseover',function() {
    var r = new XMLHttpRequest();
    r.open("POST", "index.php", true);
    r.onreadystatechange = function () {
        if (r.readyState != 4 || r.status != 200) return;
         document.getElementById('link').addEventListener('click',function() {
            window.location.assign('index.php');
        });
    };
    r.send('');
});

But I don't think that does it. 但是我不认为这样做。

Can someone point me in a direction without sharing code/solutions? 有人可以指出我的方向而无需共享代码/解决方案吗? I'm mainly interested in how I can achieve this by myself - is ajax the right way to start? 我主要对自己如何实现这一目标感兴趣-Ajax是正确的开始方式吗?

I'm looking for a javascript only solution 我在寻找纯JavaScript解决方案

You'll need a bit of a framework for subbing out the page content and managing what's in the URL without sending another request to the server and reloading the page. 您将需要一些框架来精简页面内容并管理URL中的内容, 而无需向服务器发送另一个请求并重新加载页面。

Push state can help with the URL manipulation. 推送状态可以帮助进行URL操作。

You would need to replace the body of your page (or less, depending on your markup, maybe just everything inside #container or whatever you have on the page) when you received a partial of HTML from your server via AJAX. 当您通过AJAX从服务器收到部分HTML时,您需要替换页面的正文(或更少,取决于您的标记,也许只是#container所有内容或页面上的任何内容)。

Look into how something like Rails' TurboLinks uses an approach called PJAX to quickly navigate pages. 研究一下Rails的TurboLinks之类的东西如何使用一种称为PJAX的方法来快速浏览页面。 Then the prefetching on hover could be layered on top of an approach like that. 然后,悬停时的预取可以在这样的方法之上分层。

More reading: http://pjax.herokuapp.com/ 更多阅读: http : //pjax.herokuapp.com/

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

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