/    Sign up×
Community /Pin to ProfileBookmark

cookie, ‘length’ is null or not an object

hi all,

i am a javascript, programming newbie. i use the password(cookie) script in [url]http://www.javascript-page.com/cright.html[/url] , then add the sha1 function from [url]http://pajhome.org.uk/crypt/md5[/url]

in ie6, if i press “cancel” in the password prompt, it says ‘length’ is null or not an object then break the script and go to the portected page.

here is the script;

[QUOTE]

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (“;”, offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
var arg = name + “=”;
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(” “, i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + “=” + escape (value) +
((expires == null) ? “” : (“; expires=” + expires.toGMTString())) +
((path == null) ? “” : (“; path=” + path)) +
((domain == null) ? “” : (“; domain=” + domain)) +
((secure == true) ? “; secure” : “”);
}

function DeleteCookie(name) {
var exp = new Date();
FixCookieDate (exp);
exp.setTime (exp.getTime() – 1);
var cval = GetCookie (name);
if (cval != null)
document.cookie = name + “=” + cval + “; expires=” + exp.toGMTString();
}

var d = GetCookie(“de”)
var paswd = “a9993e364706816aba3e25717850c26c9cd0d89d”

if (d == paswd) {}

if (d == null || d != paswd) {
check_in()
}

function check_in(){
var f = prompt(“Enter password to proceed.”,””);
[COLOR=Red]var h = hex_sha1(f);[/COLOR]
var thenewdate = new Date ();
thenewdate.setTime(thenewdate.getTime() + (365 * 24 * 3600 * 1000));
SetCookie(‘de’,[COLOR=Red]h[/COLOR],thenewdate);
var e = GetCookie(‘de’);

if (e == paswd) {}

else {
location = “http://www.google.com”
}

}

[/QUOTE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 05.2006 — in ie6, if i press "cancel" in the password prompt, it says 'length' is null or not an object ...[/QUOTE]
That is normal behavior. Try this:
function check_in(){
var f = prompt("Enter password to proceed.","");
if(f &amp;&amp; !/^s*$/.test(f)) { // make sure password entered
var h = hex_sha1(f);
var thenewdate = new Date();
thenewdate.setTime(thenewdate.getTime() + (365 * 24 * 3600 * 1000));
SetCookie('de',h,thenewdate);
var e = GetCookie('de');
if (e == paswd) {
location.href = "protected page url";
return true;
}
}
return false;
}
Copy linkTweet thisAlerts:
@ronkwanauthorJun 05.2006 — That is normal behavior. Try this:
function check_in(){
var f = prompt("Enter password to proceed.","");
if(f &amp;&amp; !/^s*$/.test(f)) { // make sure password entered
var h = hex_sha1(f);
var thenewdate = new Date();
thenewdate.setTime(thenewdate.getTime() + (365 * 24 * 3600 * 1000));
SetCookie('de',h,thenewdate);
var e = GetCookie('de');
if (e == paswd) {
location.href = "protected page url";
return true;
}
}
return false;
}
[/QUOTE]



thanks for your prompt reply.

now the error is gone, but still, if i press "cancel" in the password prompt, it will display the page. can i make it redirect to elsewhere if one press "cancel" instead of "ok" ?
Copy linkTweet thisAlerts:
@ronkwanauthorJun 05.2006 — anyone, please.
Copy linkTweet thisAlerts:
@phpnoviceJun 05.2006 — If it is going to the protected page, then it is doing that somewhere that you have not shown. The code I posted will not go to the protected page if the Cancel button is clicked.
×

Success!

Help @ronkwan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...