views:

220

answers:

2

I'm working on a windowing application for a website of mine. Right now, I have each individual window set up as a in the body of the site. I add new windows by literally adding the appropriate code to the end of the body element, and delete them by removing that div. (I'm using jQuery for the underlying JavaScript architecture.)

I've found that for each window, I want to be able to store some values that aren't used directly. Say I maximize a window, I would want to save its old position and size so that when I un-maximize it, it returns to its old position and not just to somewhere random. So my real question here is, would it be legal to create custom CSS attributes (knowing full well that the browser would ignore them) with the sole purpose of keeping information like this on a per-div basis? Or would this be illegal, and should I look at another alternative?

I'm certainly familiar with methods of keeping all of this in an array so the system can run blind with it, but that's lovely and prone to errors and things, and it would still be a bit tricky to work with.

Thanks

+6  A: 

I would use jQuery's data() method instead for storing temp data.

$('div#window').data('position', { x: 100, y: 200, width: 50, height: 50});
$('div#window').data('state', 'minimized');

References:

You can also make this data persistent by storing it in cookies or in session at server side and restore it on the client when page loads. This way, window’s position and state will be kept even if user reloads the page.

Koistya Navin
Both exactly what I needed, I would accept yours too if I could but I'll just upvote it since I can't. Thanks! ^_^
Nicholas Flynt
+6  A: 

jQuery has a little-known feature for this called data. With it, you can do this:

$('#mydiv').data('position', {x: 150, y: 300});

// later
var position = $('#mydiv').data('position');

If you don't like this (although I find it quite handy) it is certainly not wrong to have custom attributes, although this will make it invalid code.

Paolo Bergantino