about cookie...

hi:


i set a cookie in my ASP script..
Response.Cookies("UserName") = sUserName

Now,

Look at this javacript:


---------------------this script will auto refresh the page every 10 seconds..
<SCRIPT  LANGUAGE=javascript >

window.setTimeout("RefreshMe();", 10000);

 function RefreshMe()
 {
      var sURL;
      
      sURL="";
      sURL="test.asp";
      window.location=sURL;
            
 }
</script>
----------------

SO CAN I CHANGE TO the following??

<SCRIPT  LANGUAGE=javascript >

window.setTimeout("RefreshMe();", 10000);

 function RefreshMe()
 {
      var sURL;
      sURL="";            //in this part..i want to code if that cookie(UserName)=null
      sURL="test.asp";     //then the sURL="relogin.asp"
      window.location=sURL;
            
 }

</script>

--
so is that possible? How? or any others suggestion?



q





LVL 8
qfrenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

monvelasquezCommented:
try the code below...

--[CODE]-----------------------------------------------------------------------
function RefreshMe()
 {
     var sURL;
     <%If  IsNull(Request.Cookies("UserName") ) Then %>
     sURL="relogin.asp";
     <%Else%>
     sURL="test.asp";    
     <%End If%>
     window.location=sURL;
         
 }
--[/CODE]----------------------------------------------------------------------

or if you want a pure javascript solution, see this page..
   http://www.webreference.com/js/column8/property.html
ZontarCommented:
You can read cookies with JavaScript, but it's a bit of a pain. It's much simpler to do on the server.

Put this in the very top of your page(s):
<%
  If Request.Cookies.Key("UserName") = "" Then
    Response.Redirect "relogin.asp"
    Response.End
  End If
%>
Michel PlungjanIT ExpertCommented:
If you have an external cookie script on your webserver then reading cookies with javascript is no longer a pain
Just have
<script src="cookie.js" language="JavaScript" type="text/javascript"></script>
in the pages that needs to read the cookie
and use
function RefreshMe() {
  window.location= (getCookie('UserName'))?"test.asp":"relogin.asp"
 }

Here is the source of cookie.js

//----------------------------------
today      = new Date();
expirydate = new Date(today.getTime() + (30 * 86400000)); // 1 month from now

function getCookie(Name) {
  var search = Name + "="
  if (document.cookie.length > 0) {
     offset = document.cookie.indexOf(search)
     if (offset != -1) {
        offset += search.length
        end = document.cookie.indexOf(";", offset)
        if (end == -1) end = document.cookie.length
        return unescape(document.cookie.substring(offset, end))
     }
  }
}

function setCookie (name,value,expires,path,theDomain,secure) {
  var theCookie = name + "=" + escape (value) +
  ((expires)    ? "; expires=" + expires.toGMTString() : "") +
  ((path)       ? "; path="    + path   : "") +
  ((theDomain)  ? "; domain="  + theDomain : "") +
  ((secure)     ? "; secure"            : "");
  document.cookie = theCookie;
}

function delCookie(name,path,domain) {
  if (getCookie(name)) document.cookie = name + "=" +
     ((path)   ? ";path="   + path   : "") +
     ((domain) ? ";domain=" + domain : "") +
     ";expires=Thu, 01-Jan-70 00:00:01 GMT";
}
//--------------------------------
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

devicCommented:
hi qfren,

i don't understand why the user must wait 10 sec.?

Zontar's solution is the best in your situation.


anyway here is js solution:
====================================
<script>
window.setTimeout("RefreshMe();", 10000);

function RefreshMe()
{
    var sURL;
      if(getCookie("UserName"))
      {
            sURL="test.asp";
      }
    else
      {
            sURL="relogin.asp";
      }
    window.location=sURL;
         
}
function getCookie(name)
{
  var dc = document.cookie;var prefix = name + "=";var begin = dc.indexOf("; " + prefix);
  if (begin == -1){begin = dc.indexOf(prefix);if (begin != 0){return null;}}else{begin += 2;}
  var end = document.cookie.indexOf(";", begin);if(end == -1)end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
</script>
Michel PlungjanIT ExpertCommented:
And the difference is??? A different cookie script...
devicCommented:
hi mplungjan,

i didn't refresh the page, before posting :)
NushiCommented:
hi there.

more information can be found here.
http://hotwired.lycos.com/webmonkey/98/29/index1a_page7.html?tw=programming

Nushi.

ZontarCommented:
I think most of us who've been doing JS for very long have written or picked up a cookie script somewhere. IMO a Cookie class is one feature that's long been missing from clientside JS, and it surprises me that none of the browser vendors have ever made any kind of an effeort in this direciton.

Besides, I figured since ASP set the cookie in the first place, might as well use it to get it again. ;-)
qfrenAuthor Commented:
hi:

sorry , maybe i didnt make my Q clearly...

what i want to do is actually,
for that javascript i want to code it so that it will refresh every 10 or 15 sec , and when it refresh, it will check that cookie,UserName(where i get from Response.Cookies("UserName")..). If that cookie is null,then it will redirect to relogin.asp,else if that cookie got thing..then nothing happen...


q
devicCommented:
qfren,

you don't need to refresh to check cookie.
this script will check every 10 sec cookie, if cookie equal null, then will locate to relogin.asp
=================================================
<script>
function CheckCookie()
{
   var sURL;
    if(!getCookie("UserName"))
    {
         window.location="relogin.asp";
    }
   window.setTimeout("CheckCookie();", 10000);
}
function getCookie(name)
{
 var dc = document.cookie;var prefix = name + "=";var begin = dc.indexOf("; " + prefix);
 if (begin == -1){begin = dc.indexOf(prefix);if (begin != 0){return null;}}else{begin += 2;}
 var end = document.cookie.indexOf(";", begin);if(end == -1)end = dc.length;
 return unescape(dc.substring(begin + prefix.length, end));
}
CheckCookie();
</script>

@ Zontar
>>>it surprises me that none of the browser vendors have ever made any kind of an effeort in this direciton
yea, you are right ;)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZontarCommented:
Why do you want to check the same serverside cookie repeatedly from the clientside on the same page?

I find this puzzling since, once the page has loaded, you can't set cookies from the server.

Cookies are set using HTTP headers... You have to send headers before sending content... Yes, I'm aware of output buffering, but still... Once the page has loaded, you can't get or set cookies on the server, only on the client... Please explain what your purpose is, if there's something here that I'm missing, I'd like to know what it is, thanks.
qfrenAuthor Commented:
devic:
i will check it out the code and let u know...

Zontar :
in a login.asp page, i set the cookie....Response.Cookies("UserName") = xxxxxxxx
so....now...in other page,let say abc.asp..i wan to check every 10 or 15 seconds to make sure the cookie not null.

i want to do this because..maybe the user open 2 window ......one is the let say hello.asp..another is abc.asp..(both in same system)..so in hello.asp if the user click Logout (which i will set the Cookies("UserName") =null ). Now on abc.asp ,because it will check the value of cookie every 10 seconds...so when it found that the cookie =null(because already logout in hello.asp), it will redirect to relogin.asp to ask use to relogin again so that he/she can continue that abc.asp...


Let me know if u all still not understand my Q...sorry for the confusing...


thanks...


q
ZontarCommented:
Why not just check the cookie whenever a new page loads? That's the way it's usually done. If you unset a cookie in window A, then the user attempts to navigate elsewhere on your site using Window B, the cookie will still be unset, it own't magickally return to life or anything like that. What you want to do just seems unnecessary to me, that's all.
qfrenAuthor Commented:
Zontar:
glad to read ur suggestion....

>>Why not just check the cookie whenever a new page loads
yes..this is what i did before this..

>>If you unset a cookie in window A, then the user attempts to navigate elsewhere on your site using Window B, the cookie will still be unset

my problems is: both window A and B is openning...and user logout at A while B still opening....so in  my situation..B still openning and still can use that userID .
That y i now need to add the coding using javasript so that it will check every 10 seconds....


q


ZontarCommented:
> B still openning and still can use that userID .

For what? As soon the user tries to do anything meaningful, they'll have to load a page and then they'll be logged out again, so whatever they tried to do basically won't happen, right?
qfrenAuthor Commented:
hi:
Zontar :
but in my case the user still can view or explorer something in the pages...i also dont know why......i will try to figure out....

Anyway, devic 's comments works in my situation....

But some others experts also providing others userfull suggestion...
so i will increase my points and split the points to them...

Thansk again..

q
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.