All I want is a cookie! - Post ID 119393

User 81823 Photo


Registered User
52 posts

Hi all...

Have any of you tried to use cookies yet!

OMG - is it just me or are they impossible to work out.

I Have tried several methods all found on the net and all based around the same functions setcookie(), getcookie() and deletecookie().

The only thing is I cannot get them to work. Whenever I put them in a <script> tag the page always displays with errors and I cannot figure out why.

All I need is a cookie that will store a single colour value.

Any ideas?

Thanks in advance and hope!
Neil
User 133269 Photo


Registered User
2,900 posts

you may need to rename your page as a .php page instead of a .html page to get the scripts to run ;) Then put the setcookie() inside <?php ?> tags rather than <script> tags???
Have fun
~ Fe Pixie ~
User 364143 Photo


Guest
5,410 posts

User 81823 Photo


Registered User
52 posts

Thanks to Tom and Fe Pixie for your replies.

Fortunately I have managed to work it out. None of the samples actually worked correctly so after much head scratching and coffee in a cup :lol:, I re wrote the original samples I found as it turns out you do not need 'php' to use cookies. HTML and Javascript and a little good luck work just fine.

So I have working cookies and learnt a hell of a lot about Javascript in the process :cool:

All I have to do now is figure out how to get all my functions to call correctly from a seperate script file :(

Thanks again,
Neil
User 364143 Photo


Guest
5,410 posts

You could load the script in the head section
<script type="text/javascript" src="MyExternal.js"></script>

and call the function from withing the page body
<script>MyFunctionName()</script>

Or call the function from an onclick event.
CoffeeCup... Yeah, they are the best!
User 81823 Photo


Registered User
52 posts

Thanks for your reply Tom... I did not make it very clear!

I have managed to get my functions in a script file with no problem. The problem lies with passing variables from one function to another from within the script file.

It is hard to explain, let me try. Two functions - A and B. Both stored in a script file.

I call function A with a variable, which in turn calls function B - the problem is it does not seem to pass the variable with it and I don't know why. The VB equivalent would be local and global variables (I LOVE coding in VB as well by the way!) but I don't know if JScript or HTML can use persistant variables.

It is not vital that they are in a script file, it is just that I will be using these functions (6 of them) a lot and it would cut down on clutter as each HTML page will need all 6!

So if you do have any ideas on the solution - I am all ears.

Neil.

PS. One thing I use <script language="javascript"> and then the ref to the script file. Would this make any difference to the option you suggested?
User 364143 Photo


Guest
5,410 posts

External scripts are fine as long as you declare the script in the head section of the html, it's loaded and ready to be used. The priciple of passing a variable to any javascript function, and returning a value, is just like in VB.

W3Schools is a great source. Check out these pages on JS Functions.

http://www.w3schools.com/jS/tryit.asp?filename=tryjs_function2

http://www.w3schools.com/jS/js_functions.asp
CoffeeCup... Yeah, they are the best!
User 81823 Photo


Registered User
52 posts

Thanks Tom and sorry for the delay in replying.

I had declared in the body section instead of the head. DOH!

All working fine now.

Thanks again.
User 364143 Photo


Guest
5,410 posts

LOL, I've been there a thousand times, myself. Glad you got it working. :)
CoffeeCup... Yeah, they are the best!

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.