


Use Yii integrated pjax (pushstate+ajax) to load the page without refreshing
This article will introduce Yii to integrate pjax (pushstate ajax) to achieve non-refresh loading of pages. Let’s learn together
What is Pjax?
Pjax = history.pushState + Ajax = history.pushState + Async JS + XML(xhr?)
The BOM object history has been enhanced, mainly for operations on the history stack. In the past, only replace, go and the like would jump. Turn and refresh the entire page. Now there are pushState, replaceState and other methods that simply operate the history stack. They just modify the content in the history stack and have no side effects (the page will not jump and refresh)
PJAX Effect
The dynamically loaded content of ajax can be tracked through the url. This technique is particularly beneficial in views with two step view layouts. Loading the page without refreshing means that the response speed and user experience have been greatly improved. When there are many static scripts and common modules, the cost of reuse is saved to the greatest extent. Application examples can refer to current Google, Facebook and the new version of Weibo, which are also implemented based on HTML5 pushState. The performance of Google Plus is the most obvious. Click the navigation bar address, the arrow moves with the target, and the loaded page fades in at the same time. The effect is very dazzling.
Dirty url and Clean url
Before the advent of pjax, in order to load the page without refreshing and track it through the url, the browser needed to support window.location.hash Attributes. The content that needs to be loaded is determined by judging the address recorded after the url# anchor. The specific construction method is to write a hashchange monitoring function, and when the hash change is triggered, the content to be loaded is judged. Its disadvantage is that lower version browsers such as ie6 do not support hash, and an additional iframe needs to be built to record historical URLs to achieve forward and backward. The biggest problem is that the content generated after # will not be indexed by search engines. Google has provided a solution before, advocating the use of #! to guide the address to a request address of ?escape_fragment=url. I use twitter, facebook, Everyone on Renren, Sina Weibo and the closed Douban have seen that this kind of hash bang has been or is being used. pass#! To realize the url loaded without refreshing, because the general method is not easy to be included by search engines (such as domestic Baidu), it is called dirty url. Relatively speaking, pjax can use clean url to get the same effect, and is well compatible with various This browser is the most suitable method now
Use PHP jQuery to implement PJAX
There is no need to write a javascript plug-in based on pushState from scratch, because jQuery already has a project to implement it It is open source and can be easily implemented. At present, I have introduced it into the project under development, and it is very compatible with the original one. Not to mention the promotion of the new version of Weibo. I hope that the audience can see that this is what I look like after using it, and you will also see it after using it. It looks like this
Preparation before starting:
1. jQuery libray
2. jQ-based pjax plug-in (open source project on github) https://github. com/defunkt/jquery-pjax
3. PHP project code (for easy sharing, this article uses the yii framework for demonstration, and the actual development is similar)
1. Front-end implementation
It is really easy to use, and the jquery-pjax plug-in is well packaged. It can definitely be customized according to your preferences (such as copying the effect of g plus). The following is a basic html sample code that integrates the above steps:
<?php Yii::app()->clientScript->registerCoreScript('jquery'); Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/jquery-pjax/jquery.pjax.js');//具体以你存放pjax的实际位置为准 ?> <p id="nav"> <a href="<?php echo Yii::app()->createUrl('article/index');?>">article</a> </p> <p id="main">替换的内容</p> <script type="text/javascript"> $(document).ready(function(){ $("#main").pjax("a"); }); </script>
The ultimate goal is to replace the text in p with the id of main with the corresponding address content and url through the results returned by ajax when clicking the a tag. Updates automatically and the page does not reload. Let's start to implement the content to be processed by the backend
2. Implementation of the PHP side
The tasks that the PHP side needs to handle are mainly two: 1. Implement the layout view layout 2 .Judge the request coming from pjax
Implementation in Yii framework:
Do the following processing of action in the controller (take the index of Article as an example):
public function actionIndex() { $this->layout = '//layouts/column1'; $dataProvider = new CActiveDataProvider('Article', array( 'criteria' => array('order' => 'create_time DESC') )); if (array_key_exists('HTTP_X_PJAX', $_SERVER) && $_SERVER['HTTP_X_PJAX']) { $this->renderPartial('index', array( 'dataProvider' => $dataProvider, )); } else { $this->render('index', array( 'dataProvider' => $dataProvider, )); } }
Effect demonstration picture:
After clicking the article link or article link, the following effect page will appear:
----------------------------------------
Things to note when using jquery-pjax:
1. The returned template content cannot be plain text and needs to be wrapped with html tags
2. Plug-in How to use, please refer to the project description on github for details. The usage may be different after updating.
3. For lower version browsers that do not support pushstate, the pjax plug-in will automatically determine and use the traditional page loading mode
4. When the pjax request time of a page exceeds the set time, it will be loaded using refresh, and the relevant parameters in the plug-in need to be adjusted.
The above is what I compiled for everyone. I hope it will be useful to everyone in the future. helpful.
Related articles:
Jquery and PHP combined to implement AJAX long polling
Js ajax progress bar code when loading
The above is the detailed content of Use Yii integrated pjax (pushstate+ajax) to load the page without refreshing. For more information, please follow other related articles on the PHP Chinese website!

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment
