My code works fine but I don't know what to add to it to append it to another html page instead of to the same document's body. Yes, same website, domain name etc, I have not created the page yet. No frames. The aim is to print the text created:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to create a P element with some text, and append it to the document's body.</p>
<button onclick="myFunction()">Blanc</button>
<script>
function myFunction() {
var x = document.createElement("P");
var t = document.createTextNode("Blanc.");
x.appendChild(t);
document.body.appendChild(x);
}
</script>
<button onclick="myFunction2()">Fauve à panachures blanches</button>
<script>
function myFunction2() {
var x = document.createElement("P");
var t = document.createTextNode("Fauve à panachures blanches");
x.appendChild(t);
document.body.appendChild(x);
}
</script>
<button onclick="myFunction3()">Bonnes allures typiques</button>
<script>
function myFunction3() {
var x = document.createElement("P");
var t = document.createTextNode("Bonnes allures typiques");
x.appendChild(t);
document.body.appendChild(x);
}
</script>
</body>
</html>
If I understanding correctly, you just want to use the same javascript code on another HTML document, but without copy/pasting the code. Right?
To do this you just need to save your JS code into a file, let's call it
my-scripts.js
Now in your HTML, you can remove those scripts you pasted and just add a single
<scipt>
tag at the bottom that has a path to your new JS file you saved.You can add that script link
<script src='path/to/my-scripts.js'></script>
on any HTML file you want, just make sure the path is correctly pointing to the file relative to where the HTML file is.