JavaScript Cookie未基于URL属性写入
我编写了一个创建cookie的脚本,并根据表单数据设置名称和值?docname= getValue来自获取表单数据的脚本:JavaScript Cookie未基于URL属性写入,javascript,html,cookies,Javascript,Html,Cookies,我编写了一个创建cookie的脚本,并根据表单数据设置名称和值?docname= getValue来自获取表单数据的脚本: <script type="text/javascript"> <!-- hide from old browsers function getValue(varname) { // First, we load the URL into a variable var url = window.location.href.replace(new
<script type="text/javascript">
<!-- hide from old browsers
function getValue(varname)
{
// First, we load the URL into a variable
var url = window.location.href.replace(new RegExp( "\\+", "g" ), "%20" )
// Next, split the url by the ?
var qparts = url.split("?");
// Check that there is a querystring, return "" if not
if (qparts.length == 0)
{
return "";
}
// Then find the querystring, everything after the ?
var query = qparts[1];
// Split the query string into variables (separates by &s)
var vars = query.split("&");
// Initialize the value with "" as default
var value = "";
// Iterate through vars, checking each one for varname
for (i=0;i<vars.length;i++)
{
// Split the variable by =, which splits name and value
var parts = vars[i].split("=");
// Check if the correct variable
if (parts[0] == varname)
{
// Load value into variable
value = parts[1];
// End the loop
break;
}
}
// Convert escape code
value = unescape(value);
// Convert "+"s to " "s
value.replace("+"," ");
// Return the value
return value;
}
// end hide -->
</script>
<script>
function saveDoc()
{
var docname= getValue("docname"); // these go off another script to get form data
var save = getValue("save");
var url = window.location.href;
}
if (docname != '') {
setCookie(docname,url,730);
}
else {
// Nothing else to do
}
</script>
你用的是什么浏览器?请注意,在中使用cookies进行开发可能有点棘手,因为某些浏览器不允许您使用。相反,您需要从127.0.0.1进行测试
否则,如果看不到函数调用中的代码,就很难判断发生了什么…您使用的浏览器是什么?请注意,在中使用cookies进行开发可能有点棘手,因为某些浏览器不允许您使用。相反,您需要从127.0.0.1进行测试
否则,如果看不到函数调用中的代码,就很难判断发生了什么…您的
saveDoc
函数必须是这样的,包括if
语句:
<script type="text/javascript">
function saveDoc() {
var docname= getValue("docname"); // these go off another script to get form data
var save = getValue("save");
var url = window.location.href;
if (docname != '') {
setCookie(docname,url,730);
}
else {
// Nothing else to do
}
}
// Helps to find errors if they exist
window.onerror = function(errorMessage, url, line) {
var errorText = 'message: ' + errorMessage + '\nurl: ' + url + '\nline: ' + line;
alert(errorText);
}
</script>
您的
saveDoc
函数必须与包含if
语句的函数类似:
<script type="text/javascript">
function saveDoc() {
var docname= getValue("docname"); // these go off another script to get form data
var save = getValue("save");
var url = window.location.href;
if (docname != '') {
setCookie(docname,url,730);
}
else {
// Nothing else to do
}
}
// Helps to find errors if they exist
window.onerror = function(errorMessage, url, line) {
var errorText = 'message: ' + errorMessage + '\nurl: ' + url + '\nline: ' + line;
alert(errorText);
}
</script>
使用诸如firebug之类的工具。提取并发布cookie的http请求。检查cookie是否已在浏览器中创建。感谢@Ankit的帮助,但这不是我要找的。非常感谢。我们能看看
setCookie
函数吗?当点击按钮时它真的被调用了吗(如果是的话,那么docname、url的一些日志)。提取并发布cookie的http请求。检查cookie是否已在浏览器中创建。感谢@Ankit的帮助,但这不是我要找的。非常感谢。我们能看看setCookie
函数吗?当点击按钮时它真的被调用了吗(如果是的话,那么docname和url的一些日志)?是的@bob我会在我的帖子中添加一些如果这是你的实际代码,意外地if/else
语句超出了saveDoc
功能。我使用的是Chrome 28.0.1501.0 Canary我使用的是Chrome 28.0.1501.0 Canary非常感谢,我将尝试@bob该错误表示未捕获引用错误,未定义setCookie。不过感谢错误报告器,它让我的用户知道它是否工作,一旦它起作用了。@brandonjordan我编辑了我的答案,它应该能解决未定义的问题。它起作用了!非常感谢@bob!如果在编码等方面有什么需要,请告诉我!非常感谢,我将尝试@BOB该错误表示未捕获引用错误,未定义setCookie。但感谢错误报告器,使我的用户知道它是否工作,一旦它工作。@brandonjordan我编辑了我的答案,它应该解决未定义的问题。
工作了!非常感谢@bob!如果在编码等方面有什么需要,请告诉我!
<script type="text/javascript">
function saveDoc() {
var docname= getValue("docname"); // these go off another script to get form data
var save = getValue("save");
var url = window.location.href;
if (docname != '') {
setCookie(docname,url,730);
}
else {
// Nothing else to do
}
}
// Helps to find errors if they exist
window.onerror = function(errorMessage, url, line) {
var errorText = 'message: ' + errorMessage + '\nurl: ' + url + '\nline: ' + line;
alert(errorText);
}
</script>
<script type="text/javascript">
function setCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
}
else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
function saveDoc() {
// save your doc here
}
</script>