How to make two html files work with the same javascript

Asked

Viewed 105 times

0

How do I click a button on an html page, and in the other html page appear something with a single javascript file?

example below:

HTML 1

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Teste</title>

    <link rel="stylesheet" href="style.css">
</head>
<body>

    <button onclick="adicionar()">Teste</button>

    <script src="main.js"></script>

</body>
</html>

HTML 2

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Teste</title>

    <link rel="stylesheet" href="style.css">
</head>
<body>

    <div id="teste"></div>

    <script src="main.js"></script>

</body>
</html>

JS

var ad = document.getElementById("teste")

function teste(){
    ad.innerHTML += "Teste"
}

function adicionar(){
    function teste()
}

that I wanted, every time I clicked on the button of an html page, to appear "test" on the other html page

1 answer

0


The way you explained it, I see two ways to do what you need. Localstorage and Websocket

Localstorage

function adicionar(){

  var element = document.getElementById('teste')

  if(typeof(element) != 'undefined' && element != null){
    ad.innerHTML += localStorage.getItem('texto');
  }else{
    localStorage.setItem('texto', 'texto que quero printar');
  }

}

To use the websocket you will need a third party libraries (upload a websocket server).

  • I’m gonna come in here, vlw bro

Browser other questions tagged

You are not signed in. Login or sign up in order to post.