Solved

javascript get cookie

Posted on 2010-08-13
8
732 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
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…

706 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

14 Experts available now in Live!

Get 1:1 Help Now