1

I wrote a script that creates a cookie, and sets the name and value based on form data ?docname=,

the getValue comes from this script that gets the form data:

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

It is supposed to be created when the user clicks this link:

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

I ran an error script with the help of an answer and it said Uncaught referenceError setCookie not defined.

But when a script on another page checks for it, its not there.

Why isn't it creating the cookie?

Like this right? {

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);
}
electrikmilk
  • 1,013
  • 1
  • 11
  • 23

2 Answers2

1

Your saveDoc function must be like that including if statement:

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

Edit

Explained here more about setting cookie. And your setCookie function should be defined until saveDoc function:

<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>
Community
  • 1
  • 1
Ikrom
  • 4,785
  • 5
  • 52
  • 76
  • The Error says Uncaught ReferenceError, setCookie is not defined. – electrikmilk May 09 '13 at 17:09
  • Thanks for the error reporter though, makes it so my users know whether or not it worked, once it does work. – electrikmilk May 09 '13 at 17:32
  • @brandonjordan I edited my answer, it should solve `not defined` issue. – Ikrom May 09 '13 at 17:57
  • IT WORKED! Thanks so much @bob! if there's anything you need so far as coding, etc. goes let me know! – electrikmilk May 09 '13 at 18:11
  • Like in the last script I just updated In The post? cause it only records this much of the url _http://nsc-component.webs.com/Office/Editor/new-doc.html?docname_ It creates the cookie though – electrikmilk May 09 '13 at 18:16
  • if that can be solved it should completely fix the problem, the problem was if need to have form data _?docname_, and it keeps cutting it off, If you don't know i'll see what I can do, thanks for all of your help @bob – electrikmilk May 09 '13 at 18:35
0

What browser are you using? Note that developing with cookies in can be a little tricky because some browsers won't let you set cookies from localhost. Instead, you'll need to test from 127.0.0.1.

Otherwise, its hard to tell what's going on without being able to see the code in your function calls...

Community
  • 1
  • 1
Daniel Szabo
  • 7,181
  • 6
  • 48
  • 65