Uncaught Syntaxerror: Unexpected end of input

Asked

Viewed 15,127 times

0

My code is generating error and what I researched would be syntax error but I can’t find it here and Sublime Text 2 has no broker. Could you help me? Follow the code:

var Padrao = {

    /**
    * Init
    */
    init : function(){

        //Exibe Tela de Login/Minha Conta
        $('.login').click(Padrao.abreLogin);

        //Exibe Carrinho de Compras
        $('.carrinho-botao').click(Padrao.exibeCarrinho);

        //Chama o carrousel de Banner
        Padrao.carrouselBanner();
    },

    //Exibe Tela de Login/Minha Conta
    abreLogin : function(){
        $('.minha-conta').toggle(200);
    },

    //Exibe Carrinho de Compras
    exibeCarrinho : function(){
        $('.carrinho-aberto').toggle(200);
    },

    //Carrousel de banner
    carrouselBanner : function(){

        //recupera a qtd de elementos da lista
        var elementos = $('.destaque-banner ul li').length;

        //atribui automatico o valor de width para a ul
        $('.destaque-banner ul').css({width : 990 * elementos});


        //define e imprime a quantidade de bullets de acordo com o numero de elementos
        for(var i = 0; i < elementos; i++){
            if(i == 0){
                $('.bullets-area ul').append('<li><a href="javascript:void" index="' + parseInt(i) + '" class="sel"> x </a></li>');
            } else {
                $('.bullets-area ul').append('<li><a href="javascript:void" index="' + parseInt(i) + '"> x </a></li>');
            }
        }

        $('.bullets-area ul li a').click(function(){
            var desloca = $(this).attr('index');
            //alert(desloca);
            $('.destaque-banner ul').animate({'margin-left': desloca * -1000});
        });
    }
};

$(Padrao.init);
  • The mistake is just Uncaught SyntaxError: Unexpected end of input? There is no further detail that could help identify the error?

  • There is nothing wrong with your code, I tested it on a compiler and does not accuse errors. Where you are declaring it/calling on your page?

  • Amigo Paulo Roberto. The error that the browser was accusing was where I set href="javascript:void". It was missing (0). My fault but I found out. I appreciate the help.

  • 1

    Post it as reply @user5101 :}

  • As Pauloroberto said, post as response and select your own answer

4 answers

2

You can use the javacriptlint for that reason.

Just copy and paste the code there and it shows you syntax errors.

The site is a tool lint. Unfortunately there isn’t a wiki page for lint tools in Portuguese yet, but they are tools that analyze code for syntax errors and some bad practices. The vast majority of programming languages have such tools.

  • I tried and still does not present anything. Just some warnings that I corrected but apparently nothing out of the normal.

  • @user5101 The error is possibly in some code that was previously declared, so.

  • I’m only using the jquery library, this file I posted (padrao.js) and the Less.js library!

0

Friend, try to put a comma at the end for example:

 $('.bullets-area ul li a').click(function(){
            var desloca = $(this).attr('index');
            //alert(desloca);
            $('.destaque-banner ul').animate({'margin-left': desloca * -1000});
        });
    },
};

$(Padrao.init);

0

There is nothing wrong with the code itself. The problem is hidden in the Javascript links that are added by the code.

Instead of:

<a href="javascript:void">x</a>

Do:

<a href="javascript:;">x</a>

-2

Here’s your corrected code

var Padrao = {

    /**
    * Init
    */
    init : function(){

        //Exibe Tela de Login/Minha Conta
        $('.login').click(Padrao.abreLogin);

        //Exibe Carrinho de Compras
        $('.carrinho-botao').click(Padrao.exibeCarrinho);

        //Chama o carrousel de Banner
        Padrao.carrouselBanner();
    },

    //Exibe Tela de Login/Minha Conta
    abreLogin : function(){
        $('.minha-conta').toggle(200);
    },

    //Exibe Carrinho de Compras
    exibeCarrinho : function(){
        $('.carrinho-aberto').toggle(200);
    },

    //Carrousel de banner
    carrouselBanner : function(){

        //recupera a qtd de elementos da lista
        var elementos = $('.destaque-banner ul li').length;

        //atribui automatico o valor de width para a ul
        $('.destaque-banner ul').css({width : 990 * elementos});


        //define e imprime a quantidade de bullets de acordo com o numero de elementos
        for(var i = 0; i < elementos; i++){
            index = parseInt(i,10); // faltou um parametro aqui, parseInt(string, radix), você não tinha passado o radix
            if(i === 0){ // como era uma comparação de tipos o lint pede que seja feita uma comparação usando "==="
                $('.bullets-area ul').append('<li><a href="javascript:void" index="' + index  + '" class="sel"> x </a></li>');
            } else {
                $('.bullets-area ul').append('<li><a href="javascript:void" index="' + index  + '"> x </a></li>');
            }
        }

        $('.bullets-area ul li a').click(function(){
            var desloca = $(this).attr('index');
            //alert(desloca);
            $('.destaque-banner ul').animate({'margin-left': desloca * -1000});
        });
    }
};

$(Padrao.init);

On line 40 was complaining about the lack of the second parameter in the parseInt() by recommendation it should receive 2 parameters where the second is optional parseInt(string, radix)

Parameter - (string) Required. The string to be Parsed

Description - (Radix) Optional. A number (from 2 to 36) that represents the numeral system to be used

On line 41 the was complaining about the type of comparison so the comparison was changed from == for ===

  • 1

    He kept saying what was wrong. :)

  • 2

    parseint() works with a parameter, but as @Erloncharles put in the comments, Jslint does not "advise" the use of this form, because if you pass "0x16" (hexadecimal) as a parameter it will turn the integer 22 and not 0 or 16 as expected.

Browser other questions tagged

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