views:

354

answers:

4

Why am I getting a syntax error with this?

$(document).ready(function() {
    $.post(some_url + "ajax/get_matching_products", {
        user_id : user_id_hash,
        url  : user_page_url
    }, function(data) {
        var callback = eval("(" + data + ")");
        //alert(callback.output);
        if(callback.result == 1) {
            $("#something").prepend(callback.output);
        }
    });


    /*
     * PAGINATION
     */
    $.fn.paginate = function(options) {
        var options = jQuery.extend({
             content: 'TBODY TR',
             limit: 20
        },options);

        return this.each(function() {

            var page = {};
            page.parent = $(this),
            page.content = (page.parent.is('TABLE')) ? page.parent.find(options.content) : page.parent.children(options.content),
            page.total = Math.ceil(page.content.size() / options.limit),
            page.display = page.content.css('display'),
            page.prev = 0,
            page.next = 2;

            page.content.slice(options.limit).css('display', 'none').addClass('ui-helper-hidden');

            $(document.createElement("DIV")).addClass("link-container")[(page.parent.is('TABLE')) ? 'insertAfter' : 'appendTo'](this);

            page.linkContainer = (page.parent.is('TABLE')) ? page.parent.next('.link-container:first') : page.parent.find('.link-container:first');

            $(document.createElement("A")).addClass("pagination-link previous ui-state-default").attr('href', 'previous').attr('title', 'Previous page').attr('rel', 'nofollow').text('<').appendTo(page.linkContainer);

            for(var num=0;  num < page.total;   num++){
                var offset = num + 1,
                      min = (offset * options.limit) - (options.limit),
                      max = (offset * options.limit);

                $(document.createElement("A")).addClass("pagination-link numeric ui-state-default").attr('href', offset).attr('title', 'Page '+offset+'').attr('rel', 'nofollow').text(offset).appendTo(page.linkContainer);

                page[offset] = page.content.slice(min, [max]);
            };

            $(document.createElement("A")).addClass("pagination-link next ui-state-default").attr('href', 'next').attr('title', 'Next page').attr('rel', 'nofollow').text('>').appendTo(page.linkContainer);

            page.wraps =  page.linkContainer.find('.paginationWrap');
            page.anchors = page.linkContainer.find('A');

            page.anchors.bind('mouseenter mouseleave', function(e){

                this.self = $(this);
                (e.type === 'mouseenter') ? this.self.addClass('ui-state-hover') : this.self.removeClass('ui-state-hover');
            }).eq(1).addClass('ui-state-active');

            page.anchors.bind('click', function(e){
                 e.preventDefault();

                 if($(this).is('.ui-state-active')){
                      return false;
                 }

                 this.siblings = $(this).siblings('.ui-state-active:first');

                 if($(this).is('.previous')) {

                      if(page.prev === 0){
                           return false;
                      };

                      this.link = $(this).siblings('A[href= ' + page.prev + ']');
                      this.link.add(this.siblings).toggleClass('ui-state-active');

                      page.content.css('display', 'none').addClass('ui-helper-hidden');
                      page[page.prev].css('display', page.display).removeClass('ui-helper-hidden');

                      page.prev--,
                      page.next--;

                  } else if($(this).is('.next')) {

                      if(page.next === (page.total + 1)){
                           return false;
                      };

                      this.link = $(this).siblings('A[href= ' + page.next + ']');
                      this.link.add(this.siblings).toggleClass('ui-state-active');

                      page.content.css('display', 'none').addClass('ui-helper-hidden');
                      page[page.next].css('display', page.display).removeClass('ui-helper-hidden');

                      page.prev++,
                      page.next++;

                 } else {

                      this.link = $(this);
                      this.link.add(this.siblings).toggleClass('ui-state-active');

                      this.offset = parseInt(this.link.attr('href'));

                      page.content.css('display', 'none').addClass('ui-helper-hidden');
                      page[this.offset].css('display', page.display).removeClass('ui-helper-hidden');

                      page.prev = this.offset - 1,
                      page.next = this.offset + 1;
                 }

            });
            return this;
        });
    };
});
+2  A: 

should have read more of the code context, the comma is valid where it is.

with

var callback = eval("(" + data + ")");

what is the value of data?

Jonathan
It's the next five lines as well. They all have the same error.
Paul Manzotti
comma is a valid JavaScript statement separator and wouldn't throw a syntax error. It would only throw an error if a certain keyword followed the comma, such as `return`, `continue`, `break`, etc. Although, its usually only of use following block statements such as `if` or `for` without using opening/closing braces.
Andy E
+1, I'd come to the same conclusion about the use of `eval()` but I noticed you'd already edited your answer :-)
Andy E
+1  A: 

Put the text in the jslint page and you will get an answer.

Paul Manzotti
JSLint doesn't show the error, most likely because it occurs inside the `eval()` statement.
Andy E
on it owns that part works as well if i put dummy/static content to tables, the pagination works aswell. but when those together it gives me this error :/
adad1
A: 

Does this help finding the error?

JavaScript error in: 'clients/blabla/something/js/widget.js: (275): html error: unable to find end of script tag

adad1
A: 

Firebug gives this error:

syntax error $(function());\n

On it owns both parts works well if i put dummy/static content to tables, the pagination works aswell. but when those together it gives me this error :/

adad1