极
极核实验室

Easily Add PJAX No-Refresh Loading to Your Website

AI summary
pjax是一种利用ajax技术更新页面内容的技术,无需刷新浏览器即可获取并更新文档,同时避免重复加载js和css文件,并通过pushState功能更新URL和实现历史记录回溯。使用pjax能提升网站速度和用户体验,因为无需重新加载整个页面。实现方法包括引入jQuery和pjax库,并在文档头部和底部添加相关代码,设置局部刷新内容和加载动画。

Repost license

Request authorization before reposting and retain the authorization code, original URL, and go/affiliate links.

Original URL:https://www.geekcorelab.com/post/simply-add-pjax-without-refresh-loading-to-the-website

The commercial link is an authorized go short link that must stay on the reposted page. It records the source before sending readers to the original article.

What is pjax?

Simply put, it uses ajax technology to fetch documents from the server, updating the current page without refreshing the browser page, and ensures that files such as the page's js and css are not loaded repeatedly. Then, using the pushState feature provided by the browser, it updates the URL and ensures that users can go back to historical pages by clicking the back button.

Why use pjax?

pjax can bring a better experience. Loading a new page does not require reloading css and js, making the website faster. Because the entire page does not need to be refreshed, the page loading speed is greatly improved.

How to use pjax?

The method of use is very simple.
First, call the jQuery and pjax package code.
Nothing else needs to be set up, just go straight to the code.
Add the following resource import code inside the website's head tags:

<script type="text/javascript" src="https://cdn.gqink.cn/jQuery/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.gqink.cn/jQuery/jquery.pjax.min.js"></script>

Next, add the following code before the </body> tag.
That is, at the footer of the website.

<script type="text/javascript">
    //这里的#id代码局部刷新的内容
    $(document).pjax('a[target!=_blank]', '#id', {fragment: '#html',timeout: 8000});
        $(document).on('pjax:send', function() {
        //$('#loading').show(); // 将#loading的内容显示 加载动画
    });
    $(document).on('pjax:complete', function() {
        //$('#loading').hide(); // 将#loading的内容隐藏 加载动画
        //页面刷新后有些函数没有调用在这里可以写上你的函数让他重新加载
    });
</script>

I will not go into the loading animation here. Research it yourself; just add an emoji and write an animation.

Comments

Discussion

Be kind and keep the discussion friendly.

Complete your profile details to improve the chance of approval.

Your email will not be shown publicly.

Loading...

Markdown supported.

Discussion

0 comments

Internal link graph

Loading internal links…

View linked articles