js提取指定网站内容(一个就是代码_Load代码添加按钮和存放结构)
优采云 发布时间: 2022-01-07 14:10js提取指定网站内容(一个就是代码_Load代码添加按钮和存放结构)
在工作的过程中,遇到了一个需求,就是Js从Cookies中获取值。 js好像没有现成的方法来指定Key值来获取Cookie中对应的值。简单的实现请参考网上的代码如下:
1.服务器代码Page_Load中Cookies中写了几个值
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication_TestJS
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Cookies["DONO"].Value = "EDO1406300001";
Response.Cookies["DOID"].Value = "ABCDEFG123456";
Response.Cookies["DOSOURCE"].Value = "WUWUWUWU";
Response.Cookies["DOTYPE"].Value = "2";
}
}
}
2. 向客户端代码页添加按钮和文本框,触发并输出获取的值
function GetCookie()
{
/*获取Cookies里面存放信息 了解其字符串结构*/
var Cookies = document.cookie;
document.getElementById("").innerText = Cookies;
/*处理字符串截取出来需要的目标值*/
var target = "DONO" + "=";
if (document.cookie.length > 0)
{
start = document.cookie.indexOf(target);
if (start != -1)
{
start += target.length;
end = document.cookie.indexOf(";", start);
if (end == -1) end = document.cookie.length;
}
}
/*目标值赋值给控件*/
document.getElementById("").innerText = document.cookie.substring(start, end);
}
<br />
<br />
3.在执行结果中可以看到Cookies和第一个文本框中的结构一样,根据需要截取对应的字符串。
相关文章
猜你喜欢