alekscooper
alekscooper

Reputation: 831

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.

Upvotes: 1

Views: 17488

Answers (2)

Stefan Dacey
Stefan Dacey

Reputation: 165

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.

Upvotes: 1

Roge
Roge

Reputation: 94

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>

Upvotes: 3

Related Questions