Jump to content

Cookie to remember the size of a scalable object


DARKastheRAIN

Recommended Posts

I've got a code like this here: http://jsfiddle.net/wLMp5/6/ that allows a user to set the height of a certain object by clicking. The height is set based on the user's y mouse coordinate and scroll position. There are also a couple of objects with top margins set based on this. I need a cookie that will remember the last sizes of these things and set them that way when the user returns to the page.
Nothing I've tried so far has come close to working, so I don't think posting any of it would be particularly helpful as it mostly just amounts to fiddling around with bits of code I don't understand. I tried something using the Jquery cookie plugin, but the web host I'm using doesn't like the plugin code and freezes up everytime I try to use it, besides which I couldn't get the thing to work in the first place. I'd rather use something that doesn't use Jquery if it's possible to do this without it. The simpler, the better. If it can't be done without Jquery, then hopefully I can get something with Jquery to work without my webhost going crazy.
Sorry if it's not much of a starting point. I don't really understand cookies, no matter how much I try to find information about them.
Edited by DARKastheRAIN
Link to comment
Share on other sites

try using local/session storage instead. it's easier to use, and in nearly every way better (security, performance, bandwidth, memory size, etc.), only downside is lack of support in pre-IE 8

Link to comment
Share on other sites

looking at your fiddle there are a couple things that can be cleared up. JSfiddle doesn't make this initally apparent but when you select "onLoad" from that dropdown on the left side it wraps all that code you wrote in that javascript block into a single function and sets it to window.onload. you then wrote a '<body onload="setBookmark()">' tag in the html... which 1). you didn't need to do as JSfiddle automatically puts in a body tag, and 2). Likely overwrote everything you had in that javascript block (because you chose the onLoad setting).

 

Choosing "no wrap - in <head>" or "no wrap - in <body>" is more straightforward to understand as then it'll directly insert the code you wrote in its own <script> tag in either the head or body.

 

further more, I went in to clean up the code you got in your jsfiddle.net/5fQsn/8/ and fixed to the desired functionality; well, I rewrote all your code. The anchor tags where unnecessary, I moved all the inline css into the external sheet, and overhauled your javascript. Personally, if its possible, I like to only call the "document.getElementById" and simliar functions the least amount of times (perferbly only once). if an element won't deleted and is likely to be called upon multiple times, then theres use in remember that element instead of always trying to refind it again. Also I like to make sure that the code I write has the smallest impact in global space.If you've ever looked at the window object, you'd see there is tons of variables available in global space. and the more variables there are in a scope, the more likely that some variables might get overwritten unintentionally.

 

For example if you have a "var i;" thats being used by one piece of code, then later come in and write some other piece of code for something totally different and use var i again you might break both code sections. plus anyone else coming in (or even yourself after not looking at the code for a long time) might see that variable and would have absolutely no idea whats it for.

 

so what I do is create an object (in this example "bookmarks") and everything that has to do with it will be a property of that object. so when you see code like bookmarks.reset(), right-off-the-bat you know that reset function is for resetting bookmarks and not something else like form.reset(). and the global space doesn't see 20 some odd variables, just "bookmarks" which leads to a far less chance of accidental variable name collision.

Link to comment
Share on other sites

Awesome, thanks. It does what I want now. I'll put it into my real site and see if it works. You're the best. I've been trying to figure out how to do this for ages.

 

Edit: Alright, I've gotten it to work on my site. You're awesome. Thanks for your help.

Edited by DARKastheRAIN
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...