search
HomeWeb Front-endJS TutorialHow does JavaScript control Session operations? Method introduction

This article will introduce to you how to control Session operations using JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How does JavaScript control Session operations? Method introduction

Seeing this question, some people may raise questions. JavaScript represents the client, while Session represents the server (I don’t know if everyone can understand this).

Let me talk about the requirements first. When I am doing permission management, I need to change the module code in the Session accordingly when I click on a certain module. What implements this operation is an a tag. Of course I don’t know. Is it possible for a link button to jump to the page and modify the session at the same time? Personally, I think it is possible. If someone has made a demo, you can leave a message to explain.

a tag implements page jump. Its onclick event also executes the js method in this page. Now we are back to the problem described in the title - write a JavaScript method to modify the Session. .

In fact, this example is not difficult, but it has extraordinary personal significance to me. This example relieved a large part of my fear of AJAX.
First, write a general handler (that is, server-side code)

It should be noted that if you want to modify the Session, you need to introduce an additional namespace and implement an interface (you only need to implement it, otherwise No need to do anything)

The code is as follows:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.SessionState; 
namespace TGB.CJX 
{ 
/// <summary> 
/// 修改Session 
/// </summary> 
public class ModifySession : IHttpHandler,IRequiresSessionState 
{ 
public void ProcessRequest(HttpContext context) 
{ 
context.Response.ContentType = "text/plain"; 
context.Session["modelID"]=context.Request.QueryString["session"].ToString(); 
//context.Session["modelID"] = "1"; 
//context.Response.Write(context.Session["modelID"]); 
} 
public bool IsReusable 
{ 
get 
{ 
return false; 
} 
} 
} 
}

Did you find that the function implemented above is very simple?

The following is the client code

The code is as follows:

var xmlhttp; 
var session; 
function submit(obj) { 
//session = obj.id.substr(1, 1); 
session = obj.id.replace("model",""); 
//IE7,IE8,FF,MOZILLA,SAFARI 
if (window.XMLHttpRequest) { 
//alert("IE7,IE8,FF,MOZILLA,SAFARI"); 
xmlhttp = new XMLHttpRequest(); 
if (xmlhttp.overrideMinmeType) { 
xmlhttp.overrideMinmeType("text/xml"); 
} 
} else if (window.ActiveXObject) { 
//alert("IE5,IE6"); 
var activeName = ["MSXML2.XMLHTTP", "Miscrosoft.XMLHTTP"]; 
for (var i = 0; i < activeName.length; i++) { 
try { 
xmlhttp = new ActiveXObject(activeName[i]); 
break; 
} catch (e) { 
return; 
} 
} 
} 
if (xmlhttp == undefined || xmlhttp == null) { 
alert("当前浏览器不支持创建XMLHTTPREQUEST对象,请更换浏览器"); 
return; 
} 
xmlhttp.onreadystatechange = callback; 
xmlhttp.open("GET", "ModifySession.ashx?session=" + session, true); 
xmlhttp.send(null); 
} 
function callback() { 
//判断和服务器的交互是否完成,还要判断服务器端是否返回了数据 
if (xmlhttp.readyState == 4) { 
//表示和服务器端的交互完成 
if (xmlhttp.status == 200) { 
//alert("正确返回了数据"); 
return; 
} 
} 
}

In the callback function, I only wrote a statement to test the normal return of data, It was later commented out.
When binding events to the a tag, I initially used the method of splicing strings, which is to find the module ID and module name from the database, and then splice the statements through the following statement:

StringBuilder sbModel = new StringBuilder(); 
//将可以访问的模块进行菜单拼接 
for (int i = 0; i < dtModel.Rows.Count; i++) 
{ 
sbModel.Append("<li><a id=&#39;model" + dtModel.Rows[0]["mdlID"].ToString() + "&#39; href=&#39;SpaceWeb.aspx&#39; target=&#39;_parent&#39; runat=&#39;server&#39; onclick=&#39;submit(this)&#39;>" + dtModel.Rows[i]["mdlName"].ToString() + "</a></li>"); 
}

But writing like this is easy to make mistakes. Although I wrote test statements before copying in, and then wrote the variables to the writing position, the statements written in this way are difficult to debug.

In the process of communicating with others, I talked about the Repeater control, and suddenly realized that the process I repeated was what the Repeater control did? The control does it for us, why do we need to write such error-prone code ourselves?

<asp:Repeater runat="server" id="rptModel"> 
<ItemTemplate> 
<li><a id=&#39;model&#39;+&#39;<%#Eval("mdlID")%>&#39; href="SpaceWeb.aspx" target="_parent" runat="server" onclick="submit(this)"><%#Eval("mdlName" %></a></li> 
</ItemTemplate> 
</asp:Repeater>

If you write it like this, it will feel much clearer, because I used to splice strings before and implemented it. Using Repeater is just an idea. I don’t know if there will be any problems when splicing ids. , if there is any problem, please correct me.

So far, I have finished writing my first article about AJAX. The understanding of AJAX is just the beginning. It does not involve data interaction, so the explanation of some knowledge is still a bit pale. With the deepening of learning, the use of AJAX will not just stop at controls such as updatepanel and timer. For some examples that are not too difficult, it is better to do it yourself.

Regarding this example, you can ask why it’s so troublesome when you can actually use a LinkButton to achieve it. But I want to say that I didn’t think that a LinkButton could solve the problem at first. I just complicated the problem myself. Yes, this involves a page jump. In fact, it can be solved with LinkButton, but learning is a process. It is a pleasure to use a little confusion I made to promote my understanding of AJAX~~

The understanding of AJAX is still being progressed step by step. If there are any mistakes, please leave comments and comments

For more programming-related knowledge, please visit: Introduction to Programming! !

Statement
This article is reproduced at:脚本之家. If there is any infringement, please contact admin@php.cn delete
SpringBoot Session怎么设置会话超时SpringBoot Session怎么设置会话超时May 15, 2023 pm 02:37 PM

问题发现springboot项目生产session-out超时问题,描述下问题:在测试环境通过改动application.yaml配置session-out,经过设置不同时间验证session-out配置生效,于是就直接设置了过期时间为8小时发布到了生产环境。然而中午接到客户反应项目过期时间设置较短,半小时不操作就会话过期需要反复登陆。解决处理开发环境:springboot项目内置Tomcat,所以项目中application.yaml配置session-out是生效的。生产环境:生产环境发布是

php session刷新后没有了怎么办php session刷新后没有了怎么办Jan 18, 2023 pm 01:39 PM

php session刷新后没有了的解决办法:1、通过“session_start();”开启session;2、把所有的公共配置写在一个php文件内;3、变量名不能和数组下标相同;4、在phpinfo里面查看session数据的存储路径,并查看该文件目录下的sessio是否保存成功即可。

session php默认失效时间是多少session php默认失效时间是多少Nov 01, 2022 am 09:14 AM

session php默认失效时间是1440秒,也就是24分钟,表示客户端超过24分钟没有刷新,当前session就会失效;如果用户关闭了浏览器,会话就会结束,Session就不存在了。

PHP如何在多个文件中正确地读取和写入Session数据PHP如何在多个文件中正确地读取和写入Session数据Mar 23, 2023 am 11:12 AM

当您在使用PHP会话(Session)时,有时会发现Session在一个文件中可以正常读取,但在另一个文件中却无法读取。这可能会让您感到困惑,因为会话数据应该可以在整个应用程序中共享。本文将解释如何在多个文件中正确地读取和写入PHP会话数据。

Redis的共享session应用如何实现短信登录Redis的共享session应用如何实现短信登录Jun 03, 2023 pm 03:11 PM

1.基于session实现短信登录1.1短信登录流程图1.2实现发送短信验证码前端请求说明:说明请求方式POST请求路径/user/code请求参数phone(电话号码)返回值无后端接口实现:@Slf4j@ServicepublicclassUserServiceImplextendsServiceImplimplementsIUserService{@OverridepublicResultsendCode(Stringphone,HttpSessionsession){//1.校验手机号if

Springboot2 session设置超时时间无效怎么解决Springboot2 session设置超时时间无效怎么解决May 22, 2023 pm 01:49 PM

问题:今天项目中遇到了一个设置时间超时的问题,按SpringBoot2的application.properties更改一直不生效。解决方案:server.*属性用于控制SpringBoot使用的嵌入式容器。SpringBoot将使用ServletWebServerFactory实例之一创建servlet容器的实例。这些类使用server.*属性来配置受控的servlet容器(tomcat,jetty等)。当应用程序作为war文件部署到Tomcat实例时,server.*属性不适用。它们不适用,

JavaScript和PHP的cookie之间有哪些区别?JavaScript和PHP的cookie之间有哪些区别?Sep 02, 2023 pm 12:29 PM

JavaScriptCookie使用JavaScriptcookie是记住和跟踪偏好、购买、佣金和其他信息的最有效方法。更好的访问者体验或网站统计所需的信息。PHPCookieCookie是存储在客户端计算机上的文本文件并保留它们用于跟踪目的。PHP透明地支持HTTPcookie。JavaScriptcookie如何工作?您的服务器将一些数据发送到访问者的浏览器cookie的形式。浏览器可以接受cookie。如果存在,它将作为纯文本记录存储在访问者的硬盘上。现在,当访问者到达站点上的另一个页面时

PHP如何处理微信小程序中的session问题PHP如何处理微信小程序中的session问题Jun 02, 2023 pm 03:40 PM

近年来,微信小程序风靡全球,已经成为了许多企业和个人开发者的首选平台。在小程序的开发中,我们经常会遇到session问题,也就是如何在小程序中保存用户登录状态。这个问题对于网站开发者来说并不陌生,但在小程序中却有些不同。本文将介绍如何使用PHP解决微信小程序中的session问题。一、小程序登录过程概述小程序的登录流程与网站的登录流程类似,分为以下几个步骤:

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft