-2

below is a javascript code I want to call the script on page load.

<select onchange = "displayDate()">
          <option value='option1'>Gateway 1</option>
          <option value='option2'>Gateway 2</option>
          <option value='option3'>Gateway 3</option>
     </select>
<p id="demo"></p>

I want to call below script in select which is called on page load. I am using onchange right now which call the script when i select an option.

<script>
function displayDate() {
    document.getElementById("demo").innerHTML = Date();
}
</script>

Is there any way in which the script is called every time on page load?

random_user_name
  • 23,924
  • 7
  • 69
  • 103
Harsh
  • 109
  • 1
  • 2
  • 11

1 Answers1

0

Yes, there is. Event called onload, and has been answered here: run function when page is loaded

<!doctype html>
<html>
 <head>
    <title>onload test</title>
   <script>
  function load() {
    console.log("Evento de carregamento detectado!");
  }
  window.onload = load;
</script>
  </head>
  <body>
<p>O evento de carregamento dispara quando o documento acabou de ser carregado!</p>
  </body>
</html>
Marco
  • 2,323
  • 17
  • 23
  • 1
    If you've identified that a new question is a duplicate of another existing question which already has an answer, you shouldn't post a new duplicate answer containing the same information. Instead, the question should be flagged/closed as a duplicate of the question which already answers it. – Sam Hanley Nov 06 '17 at 16:52