admin管理员组文章数量:1321060
I have a textfield:
<input id='textfield'>
And have a script in <head>
, to get text from text field
function save(){
var text_to_save=document.getElementById('textfield').value;
}
I would like to save it (var text_to_save
) so as user will see the same text if he reload (or reopen) the page.
Thanks!
I have a textfield:
<input id='textfield'>
And have a script in <head>
, to get text from text field
function save(){
var text_to_save=document.getElementById('textfield').value;
}
I would like to save it (var text_to_save
) so as user will see the same text if he reload (or reopen) the page.
Thanks!
3 Answers
Reset to default 2you can use local storage for this:
function save(){
var text_to_save=document.getElementById('textfield').value;
localStorage.setItem("text", text_to_save); // save the item
}
Now when you reload the page you could retrieve the saved data and display it as follows:
function retrieve(){
var text=localStorage.getItem("text"); // retrieve
document.getElementById('textDiv').innerHTML = text; // display
}
a 'variant', as you put it.
You could try using cookies
Example
Save value to cookie:
document.cookie ='text_to_save='+text_to_save+';';
Read previously saved value:
var saved_text = document.cookie;
document.getElementById('textfield').value=saved_text;
Find out more about cookies here http://www.w3schools./js/js_cookies.asp
You could do this like below:
function getCookieByName( name )
{
var cookies = document.cookie,
cookie = cookies.match( '/' + name + '=(.+);/' ),
match = cookie[0];
return match;
}
var textToSave = document.getElementById('textfield').value;
document.cookie = 'mySavedText=' + textToSave;
mySavedText is the cookie name, so you could then run the function:
getCookieByName( 'mySavedText' );
and it should return the text you wanted to save.
For more information on cookie handling in Javascript check out the MDN article on it
本文标签: HTMLSave data from text field with javascriptStack Overflow
版权声明:本文标题:HTML - Save data from text field with javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742092870a2420372.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论