简体   繁体   中英

How to add a text to HTML page from javascript?

I have a textarea in HTML page ( page.html ) and it has JavaScript in it, so what I want is, through JavaScript I could add the text in another HTML page ( page2.html ) written in the textarea of page1.html . How can it be done?

To do this, you must first create a new text node. This statement creates a new text node with the text "this is a test":

var node=document.createTextNode("this is a test");

Next, you can add this node to the document. To do this, you use the appendChild method. The text can be added to any element that can contain text, but we will use a paragraph. The following statement adds the text node defined above to the paragraph with the identifier p1 :

document.getElementById("p1").appendChild(node);

You will need some kind of back-end. page.html sends ajax request to backend with updated value, page2.html sends ajax request to backend to get that value. If you don't know any back-end language (node.js, php for example), it will be hard to achieve this.

Another case if for example page2.html is "iframed" into page.html , then you can access directly with Javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM