javascript get cookie

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

dcassAsked:
Who is Participating?
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.

oshirowanenCommented:
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
Michel PlungjanIT ExpertCommented:
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
Gurvinder Pal SinghCommented:
this link has source with examples
http://www.w3schools.com/js/js_cookies.asp
you can try out also
0
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!

Michel PlungjanIT ExpertCommented:
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
Michel PlungjanIT ExpertCommented:
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

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
Michel PlungjanIT ExpertCommented:
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
dcassAuthor Commented:
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
Michel PlungjanIT ExpertCommented:
<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
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.