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:
- as soon as I click the button, test.html opens
- 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:
- as soon as I click the button, test.html opens
- 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
2 Answers
Reset to default 3In 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.