Jump to content

Cookies Why So Difficult


dzhax

Recommended Posts

I tried to use the js cookie tutorial and edited it and I can not get it to work.

<script type="text/javascript" src="elements/rate_system.js"></script>

function setSong(value){document.cookie="song=" + escape(value);}function getSong(){  c_start=document.cookie.indexOf("song=");  if (c_start!=-1)	{	c_start=c_start + c_name.length+1;	c_end=document.cookie.indexOf(";",c_start);	if (c_end==-1) c_end=document.cookie.length;	return unescape(document.cookie.substring(c_start,c_end));	}return "";}function checkSong() {	song=getSong();	if (song!=null && song!="") {		  return song;	  } else {		  alert('No song set!');	  }}

onclick="setSong('<?php echo $song[1]; ?>');"

^ this portion of the code is ok it does print the correct song name if you look at the source.

<script type="text/javascript">	 document.write(checkSong());</script>

Anyone know what i did wrong.Basically I need to set a cookie with the songs name so I can call the cookie to play that song in a mp3 player and rate the song.

Link to comment
Share on other sites

it does not know the name of the cookie as set in 'c_name' in c_start=c_start + c_name.length+1; .you have to send the cookie name to getSong as in: song=getSong('song');function checkSong() { song=getSong('song'); if (song!=null && song!="") { return song; } else { alert('No song set!'); }}now we can reference the cookie with: function getSong(c_name)function getSong(c_name){ c_start=document.cookie.indexOf("song="); if (c_start!=-1) { c_start=c_start + c_name.length+1; c_end=document.cookie.indexOf(";",c_start); if (c_end==-1) c_end=document.cookie.length; return unescape(document.cookie.substring(c_start,c_end)); }return "";}

Link to comment
Share on other sites

I made all those changes along with a few others. And still nothing. I am thinking that me taking the time out of setting it has somthing to do with it. Is that required or no?This is the Website im trying to fix. Click on the first song in the "Last 15 Songs" section. When the silver div loads, under the mp3 player that last JS is called to print the name.

<script type="text/javascript" src="elements/rate_system.js"></script>

function setSong(c_name,value){document.cookie=c_name + "=" + escape(value);}function getSong(c_name){c_start=document.cookie.indexOf(c_name +"=");if (c_start!=-1){c_start=c_start + c_name.length+1;c_end=document.cookie.indexOf(";",c_start);if (c_end==-1) c_end=document.cookie.length;return unescape(document.cookie.substring(c_start,c_end));}return "";}function checkSong(c_name) {	song=getSong(c_name);	if (song!=null && song!="") {		  return song;	  } else {		  return 'Song not set';	  }}

<a id="songLink" href="#" onclick="setVisible('darken'); setVisible('layer1'); setSong('song','<?php echo $song[1]; ?>'); return false;" target="_self">

<script type="text/javascript" src="elements/rate_system.js">	 document.write(checkSong('song'));</script>

Nothing is printing out when this last section of code is called. This is why I do not think the cookie is working.

Link to comment
Share on other sites

if you don't set the cookie date value, they only last until your current session is over.the example below is the one i got to work, when i corrected your code, so it does workcookiesong.php:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><script type="text/javascript">/*<![CDATA[*//*---->*/function setSong(value){document.cookie="song=" + escape(value);}function getSong(c_name){ c_start=document.cookie.indexOf("song="); if (c_start!=-1) { c_start=c_start + c_name.length+1; c_end=document.cookie.indexOf(";",c_start); if (c_end==-1) c_end=document.cookie.length; return unescape(document.cookie.substring(c_start,c_end)); }return "";}function checkSong() { song=getSong('song'); if (song!=null && song!="") { return song; } else { alert('No song set!'); return ""; }}/*--*//*]]>*/</script> <style type="text/css"></style></head><body><script type="text/javascript">var thissong = checkSong() document.write(thissong);</script><?php$song = array();$song[0]="overdosed on you";$song[1]="Hells Bells";?><form action="cookiesong.php" method="post"><input type="submit" onclick="setSong('<?php echo $song[0];?>');" value="<?php echo $song[0];?>" /><input type="submit" onclick="setSong('<?php echo $song[1];?>');" value="<?php echo $song[1];?>" /></form></body></html>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...