im confused about this error which i bee without todo any event like click etc. Yes i used the search here, but in all of the cases i found the situation was another problem.
The "SyntaxError: function statement requires a name" was throwing by firebug after a pagerefresh here: onGoTo:function(evt){
this is my function:
function exampleFc(ctx){
'use strict';
var goTo = $('a[href*=#]',this.$ctx);
goTo.on('click',$.proxy(this.onGoTo,this));
onGoTo:function(evt){
evt.preventDefault();
var elem = $(evt.currentTarget).attr('href');
if(elem.length > 1){
$('html,body').animate({
scrollTop: $(elem).offset().top
}, 700, function (){location.hash = elem;});
}
return false;
}
};
whats wrong? i want to call the function onGoTo by catching the click event.
im confused about this error which i bee without todo any event like click etc. Yes i used the search here, but in all of the cases i found the situation was another problem.
The "SyntaxError: function statement requires a name" was throwing by firebug after a pagerefresh here: onGoTo:function(evt){
this is my function:
function exampleFc(ctx){
'use strict';
var goTo = $('a[href*=#]',this.$ctx);
goTo.on('click',$.proxy(this.onGoTo,this));
onGoTo:function(evt){
evt.preventDefault();
var elem = $(evt.currentTarget).attr('href');
if(elem.length > 1){
$('html,body').animate({
scrollTop: $(elem).offset().top
}, 700, function (){location.hash = elem;});
}
return false;
}
};
whats wrong? i want to call the function onGoTo by catching the click event.
Share asked May 10, 2013 at 7:55 JohnDooJohnDoo 1031 gold badge2 silver badges12 bronze badges 1-
looks like you're using
this
wrong as well – Bergi Commented May 10, 2013 at 10:59
3 Answers
Reset to default 3the syntax is incorrect. that syntax should be used only inside objects.
var obj = { onGoTo: function(){ } }
in areas other than object you should use this syntax:
var onGoTo = function( ) { }; // function declaration
onGoto() // function call
or, in case of your code,
this.onGoTo = function() {}; // function declaration
this.onGoTo(); // function call
finally your code should be like this:
function exampleFc(ctx){
'use strict';
var goTo = $('a[href*=#]',this.$ctx);
goTo.on('click',$.proxy(this.onGoTo,this));
this.onGoTo = function(evt) {
evt.preventDefault();
var elem = $(evt.currentTarget).attr('href');
if(elem.length > 1){
$('html,body').animate({
scrollTop: $(elem).offset().top
}, 700, function (){location.hash = elem;});
}
return false;
}
}
ok, as Prakash GPz already says is the syntax wrong.
My solution to solved it is:
function exampleFc(ctx){
'use strict';
var onGoTo = function(evt){
evt.preventDefault();
var elem = $(evt.currentTarget).attr('href');
if(elem.length > 1){
$('html,body').animate({
scrollTop: $(elem).offset().top
}, 700, function (){location.hash = elem;});
}
return false;
};
var goTo = $('a[href*=#]',ctx);
goTo.on('click',onGoTo));
};
by the way, the function above you can use to scroll anywhere in the DOM. make sure that the scroll link have a href attribute starts with the #-Symbol and that this id exists.
best
This line cause your error
onGoTo:function(evt){
Because inside functions you can't use object syntax and have to use =
instead of :
var onGoTo = function(evt){
Note that proxy object is a part of es6 implementation.