JavaScript Cookie不基于URL属性编写

时间:2013-05-09 12:37:03

标签: javascript html html5 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 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>

应该在用户点击此链接时创建:

<a href="#" onclick="saveDoc()" role="button" class="btn" data-toggle="tooltip" data-placement="bottom" id="save" title="" data-original-title="Save"><i class="icon-folder-open"></i></button>

我在答案的帮助下运行了一个错误脚本,它说未解决的referenceError setCookie没有定义。

但是当另一个页面上的脚本检查它时,它不存在。

为什么不创建cookie?

这样吗? {

var docname= getValue("docname"); // these go off another script to get form data
    var save = getValue("save");
    var url = window.location.href;
}
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() {
    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);
}

2 个答案:

答案 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>

修改

详细解释here有关设置Cookie的信息。并且应该定义setCookie函数,直到saveDoc函数:

<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>

答案 1 :(得分:0)

您使用的浏览器是什么?请注意,使用cookie进行开发可能有点棘手,因为有些浏览器不会让你set cookies from localhost。相反,您需要从127.0.0.1进行测试。

否则,如果不能在函数调用中看到代码,很难分辨出发生了什么......