0

Got a page where users make comments on any desired post. Each post has a form with just one textarea field created dynamically from js and I'm wondering if anyone has an idea on how to save and restore the comment being typed by a user (i.e save whatever a user has typed so far before ajax refreshes the div holding all the post and comments and then restore back after the div refresh)

Tried creating something around this but not getting it to work:

<textarea id="comment_field" onKeyUp="return saveAndRestoreTypedStrings(this)"></textarea>

or  

<textarea id="comment_field" onchange="saveAndRestoreTypedStrings(' + id + ');"></textarea>

The function:

function saveAndRestoreTypedStrings(id){
   document.getElementById("post_comment").onchange = function() {
     localStorage['post_comment'] = document.getElementById(id).value;
    }
    window.onload= function(){
        if(localStorage['post_comment'])
            document.getElementById(id).value = localStorage['post_comment'];
    }   
}

To identify the form a user is on, a unique id for that form had to be passed to the saveAndRestoreTypedStrings(id) function.

For clarity, ajax only refreshes the div holding all the posts and comments made on each post. So, that div is refreshed for latest posts and comment every 3 seconds and if a user is typing to make a comment on a post and ajax reloads the div content, the user loses whatever is being typed.

Would be pleased to get ideas around this....

4

1 回答 1

0

window.onload 会在页面完全加载时启动,所以我认为这不适用于您的情况。

为什么不尝试在 AJAX 完成后使用回调函数,以便通过调用 localStorage 来“恢复”信息?

注意:对不起,我还不能评论。

于 2013-07-12T15:12:02.687 回答