Solved

javascript get cookie

Posted on 2010-08-13
8
747 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
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!

 
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

Technology Partners: 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

This article discusses four methods for overlaying images in a container on a web page
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

705 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