简体   繁体   English

在JS函数中打开一个新的HTML页面,然后在上面写一些HTML

[英]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". 我有这个main.html文件,其中的按钮类型为“按钮”。 It has a create() function that processes the onclick event. 它具有处理onclick事件的create()函数。

I also have another page test.html which is in the same folder as the main.html . 我还有另一个页面test.htmlmain.html放在同一文件夹中。

What I want: 我想要的是:

  1. as soon as I click the button, test.html opens 单击按钮后, test.html就会打开
  2. create() write some html on it, like "Hello World" create()在上面写一些html,例如“ 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. 我读到有关window.open()的信息,但我想这只是一个弹出窗口,我想要一个完整的页面。

In the main.html: 在main.html中:

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

in test.html 在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. 然后,您需要向您的test.html文件中添加一些PHP或JS。

Method 1: PHP 方法1:PHP

For example, you can get a message from the URL, that you're going to write. 例如,您可以从URL中获取要编写的消息。

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

Method 2: JS 方法2:JS

You can also write a JS function in your test file, which is called as soon as the page is loaded. 您还可以在测试文件中编写一个JS函数,该函数在页面加载后立即被调用。

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. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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