最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Open a new HTML page in a JS function and then write some HTML on it - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Dec 31, 2016 at 18:53 alekscooperalekscooper 8311 gold badge9 silver badges22 bronze badges 2
  • Concept is flawed...javascript on first page is gone when new page loads. Need more details of use case to sort out best approach. Using dynamic server side language to generate the html is one simple way – charlietfl Commented Dec 31, 2016 at 18:57
  • Please add more details and the code you have that attempts to do this so we can potentially help you more. – TylerH Commented Dec 31, 2016 at 18:59
Add a ment  | 

2 Answers 2

Reset to default 3

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.

发布评论

评论列表(0)

  1. 暂无评论