java script question...
- Started
- Last post
- 9 Responses
- wwfc
hi all - got a question I could do with getting answered if anyone knows what the answer is.
I am working on something at the moment - which has a lot of elements that have been made draggable using javascript - original source is here http://www.cross-browser.com/x/e…
what I want/need to do is add some script that will remember the position/layout of the elements when the user comes back to the site. So if they drag and move stuff around to suit their taste - the browser will remember where the elements were placed when they return later.
Is there anyway to do this - I have done a lot of looking around t'intanet but am not even sure of the terminology that I should b using to look for a solution :(
Anyone know how I can add some sort of cookie into the code to make the browser remember positions of the elements??
Be grateful for any help amigos
- stupidresponse0
if you aren't using some kind of session or login thing where this stuff could be saved server-side, i would say maybe put the data in an array and store it in a cookie
- wwfc0
...sounds like the kinda thing! Thanks for the response.
How do I do that?
- stupidresponse0
here's how to set/get cookies:
http://www.w3schools.com/JS/js_c…
there are limits to the amount of data one can store in a cookie though, so i would think of a way to store the info as compactly as possible, like each element of the array is a comma-separated x and y coordinate, and each index of the array corresponds to a specific div or something, like:
var myArray = new Array("10,100","50,500")
then you'd have to split each element to separate the x and y.
or you could make it a multi-dimensional array and do away with the whole comma thing. mabe i'll think of a better idea after i've had a nap..
http://www.w3schools.com/JS/js_o…
- wwfc0
thanks for that I'll go take a looky ;)
Please let me know if your nap provides any more inspiration stupidresponse...
...thanks for the help though - really appreciate it
- wwfc0
...hmm - that is indeed suitable for what I am trying to do - but how do I edit the cookies to work in conjunction with my current code
http://pastebin.com/m3eddc9adI get the concept but I don't think I know how to combine things...
anyone know?
- stupidresponse0
it looks like that drag and drop code you're using can have a callback function whenever an element is dropped, and it appears to provide the x and y coords as well, so you're almost there. if you view source on the demo page, you'll see the testOnDrop callback here:
xEnableDrop('drop1', testOnDrop);
and if you take a look at that function further down the page, you'll see how you can grab the x/y as arguments to that function. declare a global coords array outside of your function, and update and save it to the cookie each time an element is dropped
- wwfc0
...you make it sound very easy!!! I see the snippets you are talking about but I don't know how to tie it in together! :(
Don't s'pose you can elaborate a little more on this? I know that is cheeky - but as you know what you are talking about...
Are you able to enlighten me some more... please?
**apologies for being so slow on the uptake**
- stupidresponse0
i see in your pastebin code, you are enabling dragging but not dropping, so first add the xEnableDrop function for each of your layers. then define a function that is the callback from your xEnableDrop functions, like the testOnDrop function on the demo page. within that is where you'll manage the array of coordinates and save it to a cookie. erm.. you're on your own on that bit.
then of course when someone re-visits the page, you'll have to read that cookie and use the coords to reposition the layers. you can do it! sorry, i really need to sleep now.
- wwfc0
...thanks stupidresponse - for the help and also the words of encouragement! :)
Time to get cookie'ing!
Have a good snooze my friend