Er det her smart?
Halløj og godaften!Jeg kan ikke helt regne ud, om det her er smart...
Idéen er, at jeg har et textarea på en side, og så vil loade/save indholdet i en cookie.
Nedenstående fungere i IE og FF
<html>
<head>
<script type="text/javascript"><!--
function set_c(name, value, expires, path, domain, secure) {
var today = new Date();
today.setTime(today.getTime());
if (expires) {
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date(today.getTime() + (expires));
document.cookie = name + "=" +escape(value) +
((expires) ? ";expires=" + expires_date.toGMTString() : "" ) +
((path) ? ";path=" + path : "" ) +
((domain) ? ";domain=" + domain : "" ) +
((secure) ? ";secure" : "" );
}
function get_c(name) {
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ((!start) && (name != document.cookie.substring( 0, name.length ))) {
return null;
}
if ( start == -1 ) {
return null;
}
var end = document.cookie.indexOf(";", len);
if ( end == -1 ) {
end = document.cookie.length;
}
return unescape( document.cookie.substring(len, end));
}
function del_c(name, path, domain) {
if ( get_c( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
function c_loadText(id, name) {
var tmp = get_c(name);
if (tmp != null) {
var el = document.getElementById(id);
if (typeof(el.value) != "undefined")
el.value=tmp;
else if (typeof(el.innerHTML) != "undefined")
el.innerHTML = tmp;
}
}
function c_saveText(id, name) {
var c_name = (typeof(name) == "undefined" ? 'savedtext' : name);
var c_text = document.getElementById(id).value;
c_text = (typeof(c_text) == "undefined" || c_text == '' ? document.getElementById(id).innerHTML : c_text);
if (typeof(c_text) == "undefined" || c_text == '') {
return false;
}
set_c(c_name, c_text);
}
function initTextSave(id, name) {
if (id != null && name != null && document.getElementById(id) != null) {
c_loadText(id, name);
window.onunload = function goSave() {
c_saveText(id, name);
}
}
}
-->
</script>
</head>
<body>
<textarea id="mingemtetext" rows="10" cols="20">standard tekst</textarea>
<script type="text/javascript"><!--
initTextSave('mingemtetext', 'cookietextname');
--></script>
</body>
</html>
Comments?