|
| 1 | +--- |
| 2 | +title: Preserve the Editor content on browser back button |
| 3 | +description: Configure the Telerik RadEditor component to preserve its contents if navigating away and back using the browser's back button. |
| 4 | +type: how-to |
| 5 | +page_title: Preserve the Editor content on browser back button - RadEditor |
| 6 | +slug: editor-preserve-content-on-browser-back-button |
| 7 | +tags: editor, preserve, content, browser, back, button |
| 8 | +res_type: kb |
| 9 | +previous_url: controls/editor/how-to/preserve-content-on-back-button |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td> |
| 19 | + RadEditor for Telerik UI for ASP.NET AJAX |
| 20 | + </td> |
| 21 | + </tr> |
| 22 | + </tbody> |
| 23 | +</table> |
| 24 | + |
| 25 | +## Description |
| 26 | + |
| 27 | +**RadEditor** of Telerik UI for ASP.NET AJAX does not preserve its contents when navigating back to the page using the browser's back button. |
| 28 | + |
| 29 | +## Solution |
| 30 | + |
| 31 | +While browsers by default preserve the contents of `input` and `textarea` elements when navigating back to the page using the browser's back button, **RadEditor for ASP.NET AJAX** uses other elements, however, it can be configured additionally. |
| 32 | + |
| 33 | +Wire up the [OnClientLoad]({%slug editor/client-side-programming/events/onclientload%}) event to the Editor which will be used to **Save** and **Restore** the contents automatically. |
| 34 | + |
| 35 | +>note Can also be used with multiple Editors on the page. |
| 36 | +
|
| 37 | +>caption Example |
| 38 | +
|
| 39 | +````ASP.NET |
| 40 | +<telerik:RadEditor ID="RadEditor1" runat="server" OnClientLoad="OnClientLoad"></telerik:RadEditor> |
| 41 | +<telerik:RadEditor ID="RadEditor2" runat="server" OnClientLoad="OnClientLoad"></telerik:RadEditor> |
| 42 | +<telerik:RadEditor ID="RadEditor3" runat="server" OnClientLoad="OnClientLoad"></telerik:RadEditor> |
| 43 | +
|
| 44 | +<script> |
| 45 | + function OnClientLoad(editor, args) { |
| 46 | + // Attaching the beforeunload event to save the contents upon leaving the page |
| 47 | + addEventListener("beforeunload", (event) => { |
| 48 | + saveEditorContent(editor); |
| 49 | + }); |
| 50 | +
|
| 51 | + // Restore previous content if avaiable |
| 52 | + restoreEditorContent(editor); |
| 53 | + } |
| 54 | +
|
| 55 | + /* Helper Functions */ |
| 56 | + function saveEditorContent(editor) { |
| 57 | + editor.saveContent(); |
| 58 | + } |
| 59 | +
|
| 60 | + function restoreEditorContent(editor) { |
| 61 | + /* |
| 62 | + * setTimeout with minimal amount of delay is required as the browsers will take a |
| 63 | + * little longer to populated the input and textarea elements, and the Load event |
| 64 | + * triggers before that. |
| 65 | + */ |
| 66 | + setTimeout(function () { |
| 67 | + var storedContent = editor.get_contentHiddenTextareaValue(); |
| 68 | +
|
| 69 | + if (storedContent) { |
| 70 | + editor._initContentAreaHtml(storedContent, true); |
| 71 | + editor.set_initialContent(storedContent); |
| 72 | + } |
| 73 | + }, 10); |
| 74 | + } |
| 75 | +</script> |
| 76 | +```` |
| 77 | + |
| 78 | +### Alternative options to save the contents |
| 79 | + |
| 80 | + |
| 81 | +**Option 1**: Saving the contents while typing. |
| 82 | + |
| 83 | +Use the [keyup](https://developer.mozilla.org/en-US/docs/Web/API/Element/keyup_event) event to save the contents while typing. While there are other key events that can be used, the `keyup` event is the recommended one. |
| 84 | + |
| 85 | +````ASP.NET |
| 86 | +<script> |
| 87 | + function OnClientLoad(editor, args) { |
| 88 | + editor.attachEventHandler("onkeyup", function (e) { |
| 89 | + saveEditorContent(editor); |
| 90 | + }); |
| 91 | + } |
| 92 | +</script> |
| 93 | +```` |
| 94 | + |
| 95 | +**Option 2**: Saving the contents at specific time intervals. |
| 96 | + |
| 97 | +Another option to save the contents automatically is by using the [setInterval](https://developer.mozilla.org/en-US/docs/Web/API/Window/setInterval) method. |
| 98 | + |
| 99 | +````ASP.NET |
| 100 | +<script> |
| 101 | + function OnClientLoad(editor, args) { |
| 102 | + setInterval(function () { |
| 103 | + saveEditorContent(editor); |
| 104 | + }, 5000); |
| 105 | + } |
| 106 | +</script> |
| 107 | +```` |
0 commit comments