Solved

javascript get cookie

Posted on 2010-08-13
8
745 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

739 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