location.href="../主页/主页.html?"+"username="+encodeURI(username);
时间: 2023-05-22 21:01:31 浏览: 68
这是一个 JavaScript 代码片段,用于将当前页面的 URL 跳转到指定的主页,并将用户名编码后作为参数传递。具体实现需要在页面中定义变量 username,并使用 encodeURI() 函数对其进行编码。
相关问题
改写下面的代码,要求结果一致:function addHandleHomework(username,table,destination) { createXMLHttpRequest(); //调用创建XMLHttpRequest对象的方法 xmlHttp.onreadystatechange=callback; //设置回调函数 var myDate = new Date(); var nowtime=myDate.Format("yyyy-MM-dd hh:mm:ss"); var idtime=myDate.Format("yyyyMMddhhmmss"); var HandleHomeworkId=idtime; var StudentId=username; var HomeworkId = document.getElementById("HomeworkId").value; var Title = document.getElementById("Title").value; var Title_code = encodeURI(encodeURI(Title)); var Content = document.getElementById("Content").value; var Content_code = encodeURI(encodeURI(Content)); var HandleHomeworkCreateTime = nowtime; var url="insertonehandlehomework?table="+table+"&HandleHomeworkId="+HandleHomeworkId+"&HomeworkId="+HomeworkId+"&StudentId="+StudentId+"&Title="+Title_code+"&Content="+Content_code+"&HandleHomeworkCreateTime="+HandleHomeworkCreateTime; alert(url); xmlHttp.open("post",url,true); //向服务器端发送请求 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8"); xmlHttp.send("HomeworkId="+HomeworkId); function callback() { if(xmlHttp.readyState==4) { if(xmlHttp.status==200) { var data= xmlHttp.responseText; data=data.toString(); if(data=="successful") { alert("创建成功"); window.location.href=destination; } else if(data=="failed") { alert("创建失败,请重试"); location.reload(); } } } } }
function addHandleHomework(username, table, destination) {
createXMLHttpRequest(); // 调用创建XMLHttpRequest对象的方法
xmlHttp.onreadystatechange = callback; // 设置回调函数
const nowtime = new Date().toISOString().slice(0, 19).replace('T', ' '); // 获取当前时间
const idtime = nowtime.replace(/\D/g, ''); // 去除时间中的非数字字符
const HandleHomeworkId = idtime; // 生成处理作业的ID
const StudentId = username;
const HomeworkId = document.getElementById("HomeworkId").value;
const Title = document.getElementById("Title").value;
const Title_code = encodeURIComponent(Title); // 编码标题
const Content = document.getElementById("Content").value;
const Content_code = encodeURIComponent(Content); // 编码内容
const HandleHomeworkCreateTime = nowtime;
const url = `insertonehandlehomework?table=${table}&HandleHomeworkId=${HandleHomeworkId}&HomeworkId=${HomeworkId}&StudentId=${StudentId}&Title=${Title_code}&Content=${Content_code}&HandleHomeworkCreateTime=${HandleHomeworkCreateTime}`;
alert(url);
xmlHttp.open("POST", url, true); // 向服务器端发送请求
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");
xmlHttp.send(`HomeworkId=${HomeworkId}`);
function callback() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
const data = xmlHttp.responseText;
if (data === "successful") {
alert("创建成功");
window.location.href = destination;
} else if (data === "failed") {
alert("创建失败,请重试");
location.reload();
}
}
}
}
}
window.location.href传参
window.location.href传参是一种在URL上拼接参数值进行传递的方法。可以直接使用window.location.href进行页面跳转,并在URL上通过问号传参的方式传递参数值。例如,使用window.location.href = "./punch/clock_frm.html?modFlag=" + modFlag + '&role=' + role; 可以将modFlag和role的值传递给目标页面。\[1\]
另外,还可以使用问号在URL上拼接参数值进行传递。例如,使用window.location.href = "/static/test.html?type=" + this.selected + '&value=' + val; 可以将this.selected和val的值传递给目标页面。\[2\]
需要注意的是,为了避免传递的参数值中包含特殊字符导致URL错误,可以使用encodeURI()函数对参数值进行编码。例如,使用window.location.href = encodeURI("这里是你要跳转的地址?id=" + id); 可以将id的值传递给目标页面。\[3\]
#### 引用[.reference_title]
- *1* [转载--window.location.href传参/传值汇总](https://blog.csdn.net/weixin_47382065/article/details/120264692)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [window.location.href跳转页面并传值及接收值](https://blog.csdn.net/qq_42969135/article/details/109360734)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [jq使用window.location.href传参和接收参数](https://blog.csdn.net/lucky_Lzx/article/details/107519438)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]