Solved

javascript get cookie

Posted on 2010-08-13
8
738 Views
Last Modified: 2012-05-10
First, how do I retrieve a cookie in javascript, then how do I display the alt text with the image?
In vbscript the cookie would be 'request.cookie("webuser")("company")', and tried js code I found but not working.

<%

sCo = request.cookies("webuser")("Company")

       SQL = "SELECT * FROM T1Imgs where dblvl ='"+sCO+"' order by dbsec4,id"

       SQL_Rs.open SQL,SQL_Cn,3

       	IF NOT(SQL_Rs.EOF) THEN

	  arrResults = SQL_Rs.getrows

	END IF

      	SQL_Rs.close

      	

%>

<script language="JavaScript">

adImages=new Array(

'<%=arrResults(4,0)%>',

'<%=arrResults(4,1)%>',

'<%=arrResults(4,2)%>',

'<%=arrResults(4,3)%>');

adURL=new Array(

'<%=arrResults(4,0)%>',

'<%=arrResults(4,1)%>',

'<%=arrResults(4,2)%>',

'<%=arrResults(4,3)%>');

adALT=new Array(

'<%=arrResults(1,0)%>',

'<%=arrResults(1,1)%>',

'<%=arrResults(1,2)%>',

'<%=arrResults(1,3)%>');



thisAd = 0

imgCt = adImages.length



function rotate() {

 var theCookie=""+document.cookie;   // this is the js code I found to get the cookie, but didn't work

 var ind=theCookie.indexOf(Company);

 if (ind==-1 || theCookie=="") return ""; 

 var ind1=theCookie.indexOf(';',ind);

 if (ind1==-1) ind1=theCookie.length; 

 var sCo = theCookie.substring(ind+cookieName.length+1,ind1));

 



 if (document.images) {

    if (document.cycle.complete) {

	  thisAd++

	  if (thisAd == imgCt) {

	    thisAd = 0

	  }

	  document.cycle.src = "emenus/"+sCo+"/"+adImages[thisAd]; // + adALT[thisAd];" doesn't work

	  document.cycle.alt = "alt="+adALT[thisAd];

	}

	setTimeout("rotate()", 5000)   // 5 seconds

  }

}

function rotateLink() { window.open(adURL[thisAd]) }

</script>

<table>

    <tbody>

        <tr>

            <td valign="top" width="10" align="left">&nbsp;</td>

            <td height="520" valign="top" align="left"><strong><br />

            <a onClick="rotateLink()" href="javascript:void(0)">

            <img border="0" name="cycle"  src="/emenus/"+"<%=sCo%>"+"/"+"<%=arrResults(4,0)%>" /> </a><br />

            <br />

            <%=arrResults(1,0)%></strong></td>

        </tr>

    </tbody>

</table>

Open in new window

0
Comment
Question by:dcass
8 Comments
 

Expert Comment

by:oshirowanen
ID: 33428529
Can I recommend you use jQuery instead of raw javascript.  It will make your life a whole lot easier.

Here is an example of getting, setting and everything else you can do with a cookie using jQuery.  Just view source the page to see the code.

http://stilbuero.de/jquery/cookie/

I hope this helps.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33428533
Here.
Drop it in as
<script src="cookie.js" type="text/javascript"></script>

then use getCookie('nameOfCookie')

and
setCookie('nameOfCookie','CookieContent',expiryDate);
or
setCookie('nameOfCookie',varContaininCookieContent,expiryDate);


// cookie.js file
var cookieToday = new Date(); 
var expiryDate = new Date(cookieToday.getTime() + (365 * 86400000)); // a year

/* Cookie functions originally by Bill Dortsch */

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

function getCookie(Name) { 
   var search = Name + "=" 
   if (document.cookie.length > 0) { // if there are any cookies 
      var offset = document.cookie.indexOf(search) 
      if (offset != -1) { // if cookie exists 
         offset += search.length 
         // set index of beginning of value 
         var end = document.cookie.indexOf(";", offset) 
         // set index of end of cookie value 
         if (end == -1) end = document.cookie.length 
         return unescape(document.cookie.substring(offset, end)) 
      } 
   } 
} 
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";
}

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33428564
this link has source with examples
http://www.w3schools.com/js/js_cookies.asp
you can try out also
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33428578
Here is an untested version with some cleanup

<%
sCo = request.cookies("webuser")("Company")
       SQL = "SELECT * FROM T1Imgs where dblvl ='"+sCO+"' order by dbsec4,id"
       SQL_Rs.open SQL,SQL_Cn,3
       	IF NOT(SQL_Rs.EOF) THEN
	  arrResults = SQL_Rs.getrows
	END IF
      	SQL_Rs.close
      	
%>
<script type="text/javascript" language="JavaScript" src="cookie.js"></script>
<script type="text/javascript" language="JavaScript">
adImages=new Array(
'<%=arrResults(4,0)%>',
'<%=arrResults(4,1)%>',
'<%=arrResults(4,2)%>',
'<%=arrResults(4,3)%>');
adURL=new Array(
'<%=arrResults(4,0)%>',
'<%=arrResults(4,1)%>',
'<%=arrResults(4,2)%>',
'<%=arrResults(4,3)%>');
adALT=new Array(
'<%=arrResults(1,0)%>',
'<%=arrResults(1,1)%>',
'<%=arrResults(1,2)%>',
'<%=arrResults(1,3)%>');

var thisAd = 0
var imgCt = adImages.length;

function rotate() {
 if (!document.images) return ""; // not really needed since IE3.2 is such a long time ago ;)

 var sCo=getCookie('Company');
 if (sCo=="") return ""; 
 if (document.cycle.complete) {
	  thisAd++;
	  if (thisAd == imgCt) {
	    thisAd = 0;
	  }
	  document.cycle.src = "emenus/"+sCo+"/"+adImages[thisAd]; 
	  document.cycle.alt = adALT[thisAd];
	}
}
function rotateLink() { window.open(adURL[thisAd]) }

var tId = "";
window.onload=function() {
  tId = setInterval('rotate()',5000);
}
</script>
<table>
    <tbody>
        <tr>
            <td valign="top" width="10" align="left">&nbsp;</td>
            <td height="520" valign="top" align="left"><strong><br />
            <a onClick="rotateLink()" href="javascript:void(0)">
            <img border="0" name="cycle"  src="/emenus/"+"<%=sCo%>"+"/"+"<%=arrResults(4,0)%>" /> </a><br />
            <br />
            <%=arrResults(1,0)%></strong></td>
        </tr>
    </tbody>
</table>

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 33428594
Actually, why use cookies???

Is something setting this cookie somewhere between rotations?

If not:

<%
sCo = request.cookies("webuser")("Company")
       SQL = "SELECT * FROM T1Imgs where dblvl ='"+sCO+"' order by dbsec4,id"
       SQL_Rs.open SQL,SQL_Cn,3
       	IF NOT(SQL_Rs.EOF) THEN
	  arrResults = SQL_Rs.getrows
	END IF
      	SQL_Rs.close
      	
%>
<script type="text/javascript" language="JavaScript">
adImages=new Array(
'<%=arrResults(4,0)%>',
'<%=arrResults(4,1)%>',
'<%=arrResults(4,2)%>',
'<%=arrResults(4,3)%>');
adURL=new Array(
'<%=arrResults(4,0)%>',
'<%=arrResults(4,1)%>',
'<%=arrResults(4,2)%>',
'<%=arrResults(4,3)%>');
adALT=new Array(
'<%=arrResults(1,0)%>',
'<%=arrResults(1,1)%>',
'<%=arrResults(1,2)%>',
'<%=arrResults(1,3)%>');

var thisAd = 0
var imgCt = adImages.length;
var sCo = '<%= sCo %>';
function rotate() { // using onload instead of complete
  thisAd++;
  if (thisAd == imgCt) thisAd = 0;
  document.cycle.src = "emenus/"+sCo+"/"+adImages[thisAd]; 
  document.cycle.alt = adALT[thisAd];
}
function rotateLink() { window.open(adURL[thisAd]) }

var tId = "";
window.onload=function() {
  tId = setInterval('rotate()',5000);
}
</script>
<table>
    <tbody>
        <tr>
            <td valign="top" width="10" align="left">&nbsp;</td>
            <td height="520" valign="top" align="left"><strong><br />
            <a onClick="rotateLink()" href="javascript:void(0)">
            <img border="0" name="cycle"  src="/emenus/"+"<%=sCo%>"+"/"+"<%=arrResults(4,0)%>" /> </a><br />
            <br />
            <%=arrResults(1,0)%></strong></td>
        </tr>
    </tbody>
</table>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33428633
And even better : - popup blocker protection

<%
sCo = request.cookies("webuser")("Company")
       SQL = "SELECT * FROM T1Imgs where dblvl ='"+sCO+"' order by dbsec4,id"
       SQL_Rs.open SQL,SQL_Cn,3
       	IF NOT(SQL_Rs.EOF) THEN
	  arrResults = SQL_Rs.getrows
	END IF
      	SQL_Rs.close
      	
%>
<script type="text/javascript" language="JavaScript">
adImages=new Array(
'<%=arrResults(4,0)%>',
'<%=arrResults(4,1)%>',
'<%=arrResults(4,2)%>',
'<%=arrResults(4,3)%>');
adURL=new Array(
'<%=arrResults(4,0)%>',
'<%=arrResults(4,1)%>',
'<%=arrResults(4,2)%>',
'<%=arrResults(4,3)%>');
adALT=new Array(
'<%=arrResults(1,0)%>',
'<%=arrResults(1,1)%>',
'<%=arrResults(1,2)%>',
'<%=arrResults(1,3)%>');

var thisAd = 0
var imgCt = adImages.length;
var sCo = '<%= sCo %>';
function rotate() { // using onload instead of complete
  thisAd++;
  if (thisAd == imgCt) thisAd = 0;
  document.cycle.src = "emenus/"+sCo+"/"+adImages[thisAd]; 
  document.cycle.alt = adALT[thisAd];
}
function rotateLink(link) { 
  var w = window.open(adURL[thisAd],link.target);
  if (w) return false;
  link.href=adURL[thisAd];
  return true; // default link behaviour
}

var tId = "";
window.onload=function() {
  tId = setInterval('rotate()',5000);
}
</script>
<table>
    <tbody>
        <tr>
            <td valign="top" width="10" align="left">&nbsp;</td>
            <td height="520" valign="top" align="left"><strong><br />
            <a onClick="return rotateLink(this)" target="_blank" href="<%=arrResults(4,0)%>"><img 
              border="0" name="cycle"  src="/emenus/"+"<%=sCo%>"+"/"+"<%=arrResults(4,0)%>" /></a><br />
            <br />
            <%=arrResults(1,0)%></strong></td>
        </tr>
    </tbody>
</table>

Open in new window

0
 

Author Comment

by:dcass
ID: 33438081
Got the variable into jscript - thanks 33428633, but need a way to display the "alt" text on the page under the image as a description, making a rotating description match the rotation image.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33438372
<br /><span id="altText"><%=arrResults(1,0)%></span></strong></td>


and

function rotate() { // using onload instead of complete
  thisAd++;
  if (thisAd == imgCt) thisAd = 0;
  document.cycle.src = "emenus/"+sCo+"/"+adImages[thisAd];
  document.cycle.alt = adALT[thisAd];
document.getElementById('alttext').innerHMTL=adALT[thisAd]
}

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now