JavaScript Error Event definition

1

Good afternoon, I made a popup with jquery, and when I put it to run on element inspecte it shows an error to the following:

  

events: 1357 Uncaught ReferenceError: open_popup is not defined

I took a look at it is talk about definition, so I have no idea what this is and how to do it.

code:

<div id="banner_popup" style="display:none">
    <a href="javascript: void(0);" onclick="fecha_banner();" class="linksFechar">
        <img class="imgfechar" src="~/Images/Popup/fechar.png" border="0">
    </a>
    <a href="@Url.Action("TornarPagina", "Home")">
        <div class=" info">
        <h1><span class="linha1">Faça o </span></br><span class="linha2a"> ABC </span><span class="linha2b">do</span><span class="linha2c"> ABC </span></br><span class="linha3">sua página Inicial!</span></h1>
        <div class="button">
            Clique Aqui!
        </div>
</div>
    </a>
</div>

<script language="JavaScript">
        if(document.getElementById('banner_popup'))
            abre_popup();
</script>

<script>
    $(document).ready(function () {

        var banner = sessionStorage.getItem('banner_popup');
        if (banner != "1") {
            abre_popup();
            sessionStorage.setItem('banner_popup', "1");
        }
    });

    function fecha_banner_timeout() {
        setTimeout(function () {
            fecha_banner()
        }, 10000);
    }

    function abre_popup() {
        var banner_obj = document.getElementById('banner_popup');

        banner_obj.style.left = '';
        banner_obj.style.top = '';  

        banner_obj.style.display = '';

        fecha_banner_timeout();
    }

    function fecha_banner() {
        var banner_obj = document.getElementById('banner_popup');
        banner_obj.style.display = 'none';
    }
</script>
    
asked by anonymous 05.08.2016 / 20:39

1 answer

3

The browser will interpret the content in blocks. When he reads this block

<script language="JavaScript">
    if(document.getElementById('banner_popup'))
        abre_popup();
</script>

it will run it and there it does not find the definition of the function abre_popup . This function is defined only in the next block <script></script> and therefore is still "unknown" to the browser.

If you had your blocks in one this problem was no longer going to happen either if you had them in reverse order .

So in general, never call functions that have not yet been defined.

    
05.08.2016 / 21:19