Cookie, cookie encapsulation, cookie deletion, synchronization and asynchrony, Ajax,Ajax writing steps

Keywords: html5

cookie: session tracking technology, which is a variable stored on the visitor's computer

Set cookie s:

document.cookie = "key=value";

  The use of cookie s must have a server;

Get cookie: the obtained data is a key value pair string

The two strings are separated by semicolons and spaces

Session level cookie

The life cycle of cookie s: that is, the time when data is saved on the browser

document.cookie = "key=value;expires =" + standard date object;

  Encapsulation of cookie s

//Get cookie
function getCookie(key){
	let strCookie = document.cookie;
	let arrCookie = strCookie.split("; ");
	for(let i=0; i<arrCookie.length;i++){
		let item = arrCookie[i].split("=");
		if(item[0] == key){
			return item[1];
	return '';

function setCookie(key,value,day){
	if(day == undefined){
		document.cookie = `${key}=${value}`;
		let date = new Date();
		document.cookie = `${key}=${value};expires=`+date;
function delCookie(key){

cookie deletion

Cookies are not deleted directly, but can only be deleted laterally (emphasizing long life cycle)
    a. Set the value of key to ''
    b. Set expires to - 1

The two methods operate together

let date = new Date();
	document.cookie = "name=laowang;expires="+date;
	document.cookie = "name='';expires=-1";

Synchronous and asynchronous

Synchronization: step by step,
Asynchronous: when encountering code that needs to wait or consume time, skip the task and execute subsequent tasks first,
Until the asynchronous code consumes time, execute it again
Classification of asynchronous Codes:
      a. Timer
      b. Event body

 document.onclick = function(){//synchronization

     c. Send request receive response
     When synchronous code and asynchronous code appear   Synchronous before asynchronous


Ajax (Asynchronous JavaScript And XML), (asynchronous)   JavaScript and XML), Chinese Name: Ajax. It refers to a web development technology for creating asynchronous interactive web applications.

    * AJAX is a technology that can update some web pages without reloading the whole web page

    * Through a small amount of data exchange with the server, AJAX can make web pages update asynchronously. This means that a part of a web page can be updated without reloading the whole web page. Traditional web pages (without Ajax) must reload the whole web page if they need to update the content.

*Why use AJAX

    * More natural and smooth user experience and immediate response to user operations

    * Communicate with the Web server without interrupting user operations

    * More sensitive response to user access and achieve interaction effects similar to desktop applications

    *  Reduce network traffic and improve network efficiency by locally updating pages

Writing steps of Ajax

	//1. Pull out the phone -- > create XMLHttpRequest object
	let xhr = new XMLHttpRequest();
	//2. Dial -- > call the open method
	//"request method: get/post", "server address", asynchronous or not);"get","5ajaxStep.txt",true);
	//3. Launch -- > call the send method
	//4. Wait
	xhr.onreadystatechange = function(){
		//     Answer the phone
		if(xhr.status == 200 && xhr.readyState == 4){
			//5. Feedback information
			//Information returned by responseText
	function fun(resText){


<!DOCTYPE html>
		<meta charset="utf-8">
		<input type="text"/><span></span>
	let oText = document.querySelector("input");
	oText.onblur = function(){
		let xhr = new XMLHttpRequest();
		//get parameters: URL? Key1 = value1 & key2 = Value2"get","6ajaxGet.php?userName="+this.value,true);
		xhr.onreadystatechange = function(){
			if(xhr.status==200 && xhr.readyState==4){
	function fun(resText){
		// let oSpan = document.querySelector("span");
		// oSpan.innerHTML = resText;
		//Front and rear end separation
		let oSpan = document.querySelector("span");
		let str = resText.trim();
		if(str == "1"){
			oSpan.innerHTML = "Can register";
		}else if(str == "0"){
			oSpan.innerHTML = "Can register";
	$name = $_GET["userName"];
	$conn = mysql_connect("localhost","root","root");
	$result = mysql_query("select * from student where stu_name = '$name'",$conn);
	if(mysql_num_rows($result) == 1){
		// In the php files involved in ajax related operations,
		// echo is the keyword that returns the influence
		echo 1;
		echo 0;

Posted by cage on Tue, 14 Sep 2021 21:38:19 -0700