Recovering data from a joined string
I have a "canvas" div full of HTML, where 'canvas' here has nothing to do with an html 'canvas'. I want pack up everything relevant to div#canvas and store it as a single file, and then later read the file back and restore div#canvas as it was.
I pack up the canvas styles with
- canvas$ = $('#canvas'); // the local canvas being saved
- var canvas_margin_top = canvas$.css('margin-top');
- var canvas_margin_bottom = canvas$.css('margin-bottom');
- var canvas_margin_left = canvas$.width()/2 + 'px';
- var canvas_width = canvas$.css('width');
- var canvas_height = canvas$.css('height');
- var canvas_background_color = canvas$.css('background-color');
- // Now turn this into one continuous string in 'canvasStyle'
- canvasStyle = [' \n',
- ' position: absolute;\n',
- ' left:50%;\n',
- ' margin-top:' + canvas_margin_top + ';\n',
- ' margin-bottom: ' + canvas_margin_bottom + ';\n',
- ' margin-left:' + canvas_margin_left + ';\n',
- ' width:' + canvas_width + ';\n',
- ' height : ' + canvas_height + ';\n',
- ' background-color: ' + canvas_background_color + ';\n'
- ].join('');
After line 21 I have one long string with all the canvas style information.
This works fine except that when I read the canvasStyle string back it's not clear to me the best way to pull the individual styles back out of the string, so I can, for example, execute something like
- canvas$.css('height', canvas_height);
to apply the saved styles back to a fresh canvas, where canvas_height is the value I pulled initially on line 7 from the canvas I'm saving.
Is it clear to someone the best way to do this?
Thanks for any ideas.