2

I'm having some trouble with a javascript file, it just works on google chrome. That's my JS file, it changes the color of some DOM elements in another file called index.js. It works perfectly in Google Chrome, but in Firefox, OperaBrowser, or even in mobile browsers, it just doesn't work. Is there any changes I have to do to make it work on different browsers?

$( document ).ready(function(){

    alert('file loaded');
    // $("#bOutline1").hide();

    // $(".colorpicker").asColorPicker();
    // $(".complex-colorpicker").asColorPicker({
    //     mode: 'complex'
    // });

    var inputBarraTitulos;
    var inputMenuLateral;
    var inputCoresTextosBarraTitulos;
    var inputCoresTextosMenuLateral;
    var inputCorMenuSuperior;

    var defaultColor = "#7460ee";


    window.addEventListener("load", startCoresBarraTitulos, false);
    window.addEventListener("load", startCoresMenuLateral, false);
    // window.addEventListener("load", startCoresTextosBarraTitulos, false);
    window.addEventListener("load", startCoresTextosMenuLateral, false);
    window.addEventListener("load", startInputCorTopBar, false);

    function startCoresBarraTitulos() {
        alert('startbarratitulo');
        //cor das barras de titulo
        inputBarraTitulos = document.querySelector("#inputBarraTitulos");
        inputBarraTitulos.value = defaultColor;
        inputBarraTitulos.addEventListener("input", trocaCorBarraTitulos, false);
        inputBarraTitulos.select();

    }

    function startCoresMenuLateral() {

        //cor do menu lateral
        inputMenuLateral = document.querySelector("#inputMenuLateral");
        inputMenuLateral.value = defaultColor;
        inputMenuLateral.addEventListener("input", trocaCorMenuLateral, false);
        inputMenuLateral.select();
    }

    // function startCoresTextosBarraTitulos() {

    //     //cor do menu lateral
    //     inputCoresTextosBarraTitulos = document.querySelector("#inputCoresTextosBarraTitulos")
    //     inputCoresTextosBarraTitulos.value = defaultColor
    //     inputCoresTextosBarraTitulos.addEventListener("input", trocaCorTextosBarraTitulo, false)
    //     inputCoresTextosBarraTitulos.select()
    // }

    function startCoresTextosMenuLateral() {

        //cor do menu lateral
        inputCoresTextosMenuLateral = document.querySelector("#inputCoresTextosMenuLateral");
        inputCoresTextosMenuLateral.value = defaultColor;
        inputCoresTextosMenuLateral.addEventListener("input", trocaCorTextosMenuLateral, false);
        inputCoresTextosMenuLateral.select();
    }

    function startInputCorTopBar() {      

        inputCorMenuSuperior = document.getElementById("inputCorMenuSuperior");
        inputCorMenuSuperior.value = defaultColor;
        inputCorMenuSuperior.addEventListener("input", trocaCorTopBar, false);
        inputCorMenuSuperior.select();
    }

    // converter RGB em HEX
    function rgbToHex(total) {
        var total = total.toString().split(',');
        var r = total[0].substring(4);
        var g = total[1].substring(1);
        var b = total[2].substring(1,total[2].length-1);
        return ("#"+checkNumber((r*1).toString(16))+checkNumber((g*1).toString(16))+checkNumber((b*1).toString(16))).toUpperCase();
    }
    function checkNumber(i){
        i = i.toString();
        if (i.length == 1) return '0'+i;
        else return i;
    }




    function trocaCorBarraTitulos(event) {

        const barraTitulos = document.getElementById("cTitulo");
        const tituloSelecaoCores = document.getElementById("tituloSelecaoCores");
        const FotoPerfil = document.getElementById("FotoPerfil");
        const flexNoBlock = document.getElementById("FlexNoBlock");

        tituloSelecaoCores.style.backgroundColor = event.target.value;
        FotoPerfil.style.backgroundColor = event.target.value;
        barraTitulos.style.backgroundColor = event.target.value;
        flexNoBlock.style.backgroundColor = event.target.value;

        const corBarraTitulos = tituloSelecaoCores.style.backgroundColor; //recebe a cor geral das barras de titulo

        document.getElementById("RecebeCorBarraTitulos").value = rgbToHex(corBarraTitulos);
        const RecebeCorBarraTitulos = document.getElementById("RecebeCorBarraTitulos").value;
        console.log(RecebeCorBarraTitulos);

        $.ajax({
            url:'cor_enviar.php',
            type:'POST',
            data: {RecebeCorBarraTitulos: RecebeCorBarraTitulos}, 
            success:function(data){
                $("#bOutline1").show();
                alert(data);
            },
            error: function(data){
                alert("erro");
            }
        });

    }



    function trocaCorMenuLateral(event){
        const leftSidebar = document.getElementById("leftSidebar");

        const corMenuLateral = leftSidebar.style.backgroundColor;
        leftSidebar.style.backgroundColor = event.target.value;

        document.getElementById("RecebeCorMenuLateralFundo").value = rgbToHex(corMenuLateral);
        const RecebeCorMenuLateralFundo = document.getElementById("RecebeCorMenuLateralFundo").value;
        console.log(leftSidebar.style.backgroundColor);

        $.ajax({
            url:'cor_enviar.php',
            type:'POST',
            data: {RecebeCorMenuLateralFundo: RecebeCorMenuLateralFundo}, 
            success:function(data){
                $("#bOutline1").show();
                alert(data);
            },
            error: function(data){
                alert("erro");
            }
        });

    }

    function trocaCorTopBar(event){

        const topBar = document.getElementById("topbar");

        topBar.style.backgroundColor = event.target.value;
        const corTopBar = topBar.style.backgroundColor;

        document.getElementById("RecebeCorTopBar").value = rgbToHex(corTopBar);
        const RecebeCorTopBar = document.getElementById("RecebeCorTopBar").value;
        console.log(RecebeCorTopBar);

        $.ajax({
            url:'cor_enviar.php',
            type:'POST',
            data: {RecebeCorTopBar: RecebeCorTopBar}, 
            success:function(data){
                $("#bOutline1").show();
                alert(data);
            },
            error: function(data){
                alert("erro");
            }
        });

    }

    function trocaCorTextosMenuLateral(event){
        const textoLeftSidebar = document.getElementById("textColor");
        const corTextoMenuLateral = textoLeftSidebar.style.color;

        textoLeftSidebar.style.color = event.target.value;

        document.getElementById("RecebeCorTextoMenuLateral").value = rgbToHex(corTextoMenuLateral);
        const RecebeCorTextoMenuLateral = document.getElementById("RecebeCorTextoMenuLateral").value;
        console.log(RecebeCorTextoMenuLateral);

        $.ajax({
            url: 'cor_enviar.php',
            type: 'POST',
            data: {RecebeCorTextoMenuLateral: RecebeCorTextoMenuLateral},
            success: function(data){
                $("#Outline1").show();
                alert(data);
            },
            error: function(data){
                alert("erro");
            }
        })

    }

});
trsp
  • 78
  • 1
  • 1
  • 11
  • Do you get any error messages in your developer window console? And what do you mean by it doesn't work? – Rob Kwasowski Jan 26 '20 at 01:32
  • no, there's no message at console, neither in google chrome nor firefox. The file looks like just read the first line, just the first alert is read, the rest of the code, no. – trsp Jan 26 '20 at 01:33
  • Try clearing your cache. – GetSet Jan 26 '20 at 01:37
  • already have done that, nothing changed – trsp Jan 26 '20 at 01:37
  • 2
    I think the problem is that you are using the "load" event, and by the time the jQuery.ready event runs the window.load event has already been fired so nothing runs. Just call the functions directly, you are already using $.ready – sney2002 Jan 26 '20 at 01:41

1 Answers1

1

You're setting load event listeners on the window inside your $(document).ready() function.

This is non-deterministic and unreliable. At the time you set these listeners, the load event may have already fired.

This could easily vary from browser to browser or other factors.

Move the window.addEventListener("load",...) calls outside the $(document).ready().

There may be other problems in the code, but I'm out of time for the moment and just wanted to let you know about this one problem.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • 2
    Note: moving the `addEventListener` lines outside won't work, since they won't have access to the functions, which are local to the `ready` callback. Just remove this `$(document).ready` (but keep `(function() { ... })`) and add `()` at the end to execute it. You should be left with an [IIFE](https://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript): `(function() { /* All code here */ })();` – blex Jan 26 '20 at 01:45
  • 1
    Thank you very much for your time guys, it's working now. It was about the load event, but the IIFE was necessary either. So again thank you!!! – trsp Jan 26 '20 at 02:01
  • 2
    @ThiagoRobles The IIFE is not necessary. It's just safer to keep your variables contained. If you don't use one, they will be global. And if another script on the page is using variables with the same name, they will collide and you may get weird bugs. Most of the time, it will be ok. But better be safe than sorry – blex Jan 26 '20 at 03:03