简体   繁体   中英

Open a new HTML page in a JS function and then write some HTML on it

I have this main.html file which has a button of type "button". It has a create() function that processes the onclick event.

I also have another page test.html which is in the same folder as the main.html .

What I want:

  1. as soon as I click the button, test.html opens
  2. create() write some html on it, like "Hello World"

I don't understand how to do it. I read about window.open() but I guess this is just a pop up, and I want a full-fledged page.

In the main.html:

 <input type='button' onclick='location.href=test.html' value='click me'> 

in test.html

 function create() { // Write something in the page } 
 <body onload='create()'> </body> 

function create()
{
    window.location = 'test.html';
}

This is your starting point. Then, you need to add some PHP or JS to your test.html file.

Method 1: PHP

For example, you can get a message from the URL, that you're going to write.

Main file: function create() { window.location = 'test.php?p=Hello%World'; } Test file:

Method 2: JS

You can also write a JS function in your test file, which is called as soon as the page is loaded.

Main file:

 function create() // Create function on 1st page { window.location = 'test.html'; } 

Test file:

 <head> <script type="text/javascript"> function message() { document.getElementById('container').innerHTML = 'Hello world'; } </script> </head> <body onload="message();"> <div id="container"></div> </body> 

Hope it helped.

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