How and why getElementById does not execute on page load?

0

Business problem: I will create a system that will run on a serverless platform. It can be a local administration area interface (a client for a service), or a Mobile application. The application will dynamically load content for ease of maintenance and upgrade, using only client-side technology (CSS and javascript). Reasons are not the case for not polluting the topic.

Technical problem: The document.write () object has a bad reputation for overwriting the document, and building a document with the write function is very complicated. So we usually choose to update content by getElementById, although a lot of people recommend using AJAX or Jquery that for certain reasons do not work out of the server for the scope of this project. However, getElementById does not load without calling a function in the code below, or even calling onload on Body:

<!DOCTYPE html>
<html>
<head>
    <title></title>

</head>
<body>
<div id="recebeProduto1"></div>


<script type="text/javascript">

        getElementById("recebeProduto1").innerHTML = "teste";

    </script>
</body>
</html>

Question: How and why does not getElementById execute on page load?

or

What alternatives to loading content dynamically using Client-Side technology?

    
asked by anonymous 02.03.2017 / 15:42

1 answer

4

change your code to document.getElementById("recebeProduto1") because getElementById is a method of object document

<!DOCTYPE html>
<html>
<head>
    <title></title>

</head>
<body>
<div id="recebeProduto1"></div>


<script type="text/javascript">

        document.getElementById("recebeProduto1").innerHTML = "teste";

    </script>
</body>
</html>
    
02.03.2017 / 16:08