[removed]() vs inserting DOM nodes: preserve form information?

后端 未结 4 1864
终归单人心
终归单人心 2020-12-19 08:05

Consider two web pages with the following in their body respectively:




        
相关标签:
4条回答
  • 2020-12-19 08:20

    Normally, I prefer to create a DOM element and insert it - especially since I can insert it exactly where I want in the page (i.e. use object.appendChild or body.appendChild). It also does not matter if the insertion is during page load or later so I can use a generalized insertion function and use it any time, during or after the full page load.

    However, during the load process, if you are inserting content which includes javascript (i.e. a SCRIPT type="text/javascvript"......./SCRIPT sequence), you MUST use document.write in order to have the script execute.

    • Using document.body.appendChild(theInsertionObject) will properly add the insertion at the current point in the load process including the SCRIPT tag sequence, but it will NOT execute the javascript.

    • If you use document.write(theInsertionCode), the code will be inserted at the current point in the load process, and any embedded SCRIPT tag sequence will be executed before the remainder of the page is loaded.

    0 讨论(0)
  • 2020-12-19 08:27

    Would it be possible for you to add a hook which you could use to target the new textarea into? eg. a <div>

    <div id="addtextarea"></div>
    
    var e = document.getElementByID('addtextarea');
    e.innerHTML = '<textarea></textarea>';
    

    Then if you needed to remember the contents you could take either the innerHTML value to include the HTML or just the text node value of the textarea to extract the text.

    0 讨论(0)
  • 2020-12-19 08:31

    I believe the document.write version actually blows away an existing content on the page. Ie, the body and script tags will no longer be there. That is why people usually use appendChild.

    Keeping the text or not is very browser specific. I wouldn't bet that Firefox would not change it's behavior on that in a future version, either. I would suggest implementing an alert dialog when the user trys to navigate away from the page and there is unsaved content in edit fields. Usually you would do this with an unload event.

    0 讨论(0)
  • 2020-12-19 08:37

    document.write() won't blow away the page content as long as it is executed inline as the page is rendered. Online advertising makes extensive use of document.write() to dynamically write adverts into the page as it loads.

    If however, you executed the document.write() method at a later time in the page history (after the body is completely rendered) then as Chase says, it would blow away the existing body and display the argument to document.write().

    Other than that I agree that the preserving forms behaviour is really pretty browser specific, and not something you should rely on in many cases. It's a feature there to help the user rather than something for developers to be aware of or attempt to utilize.

    0 讨论(0)
提交回复
热议问题