how to fix error "Expected identifier, string or number" - javascript

I have been fighting this issue for couple of days now and i am not able to resolve this issue because the line where it says the error is is BLANK. my JS code is below:
$(document)
.ready(
function() {
// THE DEBUGGER SAYS THE ERROR IS IN THIS LINE"(script1028
// expected identifier string or number)".
$(document).ajaxStart(function() {
$('#overlay').show();
});
$(document).ajaxStop(function() {
$('#overlay').hide();
});
var html = "";
$('#zipcode,#telephone').autotab_magic().autotab_filter(
'numeric');
$("#backtoTopholder").click(function() {
window.scrollTo(0, 0);
})
preload([ '../img/AjaxLoader.gif' ]);
$("#startover").click(function() {
})
$("#backtoTopholder").hide();
$(function() {
$(window).scroll(function() {
if ($(this).scrollTop() > 100) {
$('#backtoTopholder').fadeIn();
} else {
$('#backtoTopholder').fadeOut();
}
});
$('#backtoTopholder a').click(function() {
$('body,html').animate({
scrollTop : 0
}, 800);
return false;
});
});
$(".addressMatch").click(function() {
alert("index");
});
preload([ '../img/step1.png', '../img/step2.png',
'../img/step3.png', '../img/step4.png',
'../img/step5.png', '../img/step6.png',
'../img/add_to_cart_button_off.png',
'../img/add_to_cart_button_on.png' ]);
$.ajaxSetup({
cache : false,
global : true
});
$("#existingCustomer").change(function() {
switch (this.value) {
case "No":
$("#WTNRow").hide();
break;
case "Yes":
$("#WTNRow").show();
break;
default:
break;
}
})
$("#plus4")
.click(
function() {
if (!$("#address").val()
|| !$("#city").val()
|| !$("#state").val()) {
alert("In order to do verify the zip code you need to fill out the address,city and state");
return false;
}
var data = $("#searchPackages")
.serialize();
var requestZipCode = $
.ajax({
url : "classes/Dispatcher.php?Ziplookup=1",
type : "POST",
dataType : "text",
cache : 'false',
data : data
});
requestZipCode
.success(function(data) {
var result = data;
if ($.trim(result) == "not found") {
$("#zipCodeReturnmsg")
.html("");
$("#zipCodeReturnmsg")
.html(
"<font color=\"red\">could retrieve a valid zipcode, please review your address and try again.Please select a unit Suffix.</font>");
} else {
$("#zipCodeReturnmsg")
.html("");
$("#zipcode")
.val(
$(
"#zipcode")
.val()
+ $
.trim(result));
$("#zipCodeReturnmsg")
.html(
"<font color=\"green\">zip code retrieved successfully.</font>");
}
});
requestZipCode
.fail(function(jqXHR, error,
errorThrown) {
$("#test").html(
jqXHR.responseText);
});
})
});
I overlooked for extra commas and reserved words and i believe i didn't miss any. Any help to fix this will be very thankful.
I am suing jquery version 1.10.2.
thank you very much

The indentation makes it very hard to read what's going, I believe you're missing a few semicolons. For example:
$("#backtoTopholder").click(function(){
window.scrollTo(0,0);
})
preload(['../img/AjaxLoader.gif']);
should this be:
$("#backtoTopholder").click(function(){
window.scrollTo(0,0);
});
preload(['../img/AjaxLoader.gif']);
?

There is nothing wrong with what you posted here. Your error is elsewhere. Fiddly proof here.
// some code because Stack Overflow complains to JSFiddle links
// without accompanying code...
// even though I just posted the exact same code that OP did
// only with filler functions to stub for code he did not post
// >.<

Related

Apex incorrect apex.widget.report.init generates "SyntaxError: Unexpected token ')'"

I'm new in Apex and I have intermitent javascript issues. Sometimes, the javascript code look like
apex.jQuery( function() {
apex.page.init( this, function() {
apex.jQuery.when.apply( apex.jQuery, apex.page.loadingDeferreds ).done(function() {
try {
(function(){apex.jQuery('body').addClass('t-PageBody--leftNav');})();
(function(){apex.widget.regionDisplaySelector("13355680312108033",{"useSlider":true,"mode":"standard"});})();
(function(){apex.widget.checkboxAndRadio("#P3_CONSENT","radio",{"ajaxIdentifier":"AfBpnXwbQbQJKq92jobIFY0jBsQKfTEyAmzSc_BoiFj0TY2d9RzfdClf0P8N90vP","optimizeRefresh":true,"inputName":"P3_CONSENT"});})();
(function(){apex.widget.yesNo("P3_FOLLOW_UP");})();
(function(){apex.widget.report.init("R13412202309260784","c_7JGZTlVYTYHuH54kgss6ofhYAQBr3aH37LWWLa7nk",{"styleChecked":"#dddddd","internalRegionId":"13412202309260784"});})();
(function(){apex.widget.report.init("R8460592047638582","8XMgrAIovIb5P-3FC2ADmJorlcBCHA6-5hl_uVVGhtQ",{"styleChecked":"#dddddd","internalRegionId":"8460592047638582"});)();
(function(){apex.widget.report.init("R9396129057606977","TpBZvCw_LAhYxzQ2cZmznnJbjRu7z6ACD1CV3lFQdns",{"styleChecked":"#dddddd","internalRegionId":"9396129057606977"});})();
(function(){apex.widget.report.init("R13355099181075640","c_7JGZTlVYTYHuH54kgss7ItG3KLYkF7JYiSM8NcMgc",{"styleChecked":"#dddddd","internalRegionId":"13355099181075640"});})();
(function(){apex.widget.report.init("R13356294220216178","c_7JGZTlVYTYHuH54kgss4h64IWClBbqYkErs41b7AI",{"styleChecked":"#dddddd","internalRegionId":"13356294220216178"});})();
apex.theme42.initializePage.noSideCol();
// this hides the 'show all' bit in the region selector (looks like tabs) //
$('.apex-rds li:first-child').remove();
$('.apex-rds li:first-child').addClass('apex-rds-first');
$('.apex-rds li:first-child > a').trigger('click');
$(".apex-rds [href='#R4782516382438893']").trigger('click');
if (apex.item('P3_REGION').getValue() == "R2") {
$(".apex-rds [href='#R4794523947034397']").trigger('click');
}
if (apex.item('P3_REGION').getValue() == "R3") {
$(".apex-rds [href='#R4795718986174935']").trigger('click');
}
apex.item( 'P3_CONSENT' ).setFocus();
} finally {
apex.event.trigger(apex.gPageContext$,'apexreadyend');
}
});
});
});
The row for 8460592047638582 does not end in });})(); but in });)();
A } is missing. I have no idea where to look to solve this issue. Any ideas?

Stop a custom.js function

my admin-ajax.php file is causing quite a bit of server load and ending up with a 400 request. Ive honed in on the culprit which appears to be "action=wishlist_counter" caused by woocommerce. Although the cart is disabled its still causing issues. The code in question is found in my themes custom.js file and appears as is below;
/*************************************************************
WOOCOMMERCE WISHLIST
*************************************************************/
function to_wishlist_count() {
$.ajax({
type: 'POST',
url: to_ajaxurl,
data: {action : 'wishlist_counter'},
success:function(data) {
$('.to-wishlist-number').html(data);
if(parseInt(data) === 0 ) {
$('.to-wishlist-counter').animate({top:'-100%'}, 0);
} else {
$('.to-wishlist-counter').animate({top:0}, 400);
}
}
});
}
$(document).ready(function() {
to_wishlist_count();
});
$(document).on('click', '.product-remove a', function() {
setTimeout(function() {
to_wishlist_count();
}, 1500);
});
$(document).on('click', '.add_to_wishlist', function(){
var $this = $(this);
var productAdded = $this.parents('li.product').find('h3').text();
if (productAdded == '') {
productAdded = $('h1').text();
}
$('.wishlist-notification .item-product').html('"'+productAdded+'"');
$this.parents('li.product').find('.product-img-wrap div.loading').fadeIn(100);
setTimeout(function(){
$('.to-wishlist-number').html(parseInt($('.to-wishlist-number').html())+1);
$this.parents('li.product').find('.product-img-wrap div.loading').fadeOut(100);
$('#header .wishlist-notification').fadeIn(400);
setTimeout(function(){
to_wishlist_count();
$('#header .wishlist-notification').fadeOut(400);
},2250);
},500);
});
})(jQuery);
What would be the best way of removing this code or ceasing it from its dastardly activities. Removing it from the file? or perhaps a function of sorts?

jQuery Post Submitting Twice

I have a form that uses a formvalidation.io class. I Have a if statement that says if the field is valid use a jQuery post to submit the data and prepend the data on a table. For Some reason it is submitting twice. If I remove the $.post and prepend the data under the if statement the prepend only does one time. I have the e.preventDefault() at the start of the function. That seams to be the only solution on other forums. Any Help Appreciated:
barcode: {
onSuccess: function(e, data) {
e.preventDefault();
setTimeout(function() {
if (!$('#move_bin').data('formValidation').isValidField('bin')) {
$("#barcode").val("");
$('#move_bin').data('formValidation').updateStatus('barcode', 'NOT_VALIDATED');
$('#move_bin').data('formValidation').updateStatus('bin', 'INVALID');
} else {
setTimeout(function() {
if ($('#move_bin').data('formValidation').isValidField('barcode')) {
$.post( "/?a=scantobin", { bin: $("#bin").val(), barcode: $("#barcode").val() }, function( data ) {
if(data.valid == true){
$("#bin_info tbody").prepend("<tr><td>" + $("#bin").val() + "</td><td>" + $("#barcode").val() + "</td></tr>");
$('#move_bin').data('formValidation').resetField('barcode', true);
$("#barcode").focus();
} else {
alert("Internal Error");
}
}, "json");
}
}, 750);
}
}, 500);
},
There is nothing wrong in your code, may be somewhere in your project you are invoking that function twice.
http://formvalidation.io/examples/form-submit-twice/
This helped me out. I had to place the code in a Form Level instead of the field level on the formvalidation.

Ajax call onchange of date

I am using daterangepicker.js, onchange of date i have to call the getchart method
when i change the date i need to call a getchart(), can anyone help me out in this
here is my code:
$(document).ready(function() {
$('#dateRangeSelector').daterangepicker(); //textbox id
var start = Date.today().setTimeToNow();
var end = Date.today().setTimeToNow();
var displayValue = start.toString("yyyy/MM/dd") + " - " + end.toString("yyyy/MM/dd");
$('#dateRangeSelector').val(displayValue);
var host = '<?=$host ?>'
var dateRangeSelector = $("#dateRangeSelector").val();
var dates = dateRangeSelector.split(" - ");
var value = "'"+dates.join("' , '")+"'";
var array = value.split(',');
var startTime = array[0];
var endTime = array[1];
$.get(SITE_URL + 'admin/SystemMonitor/charts/getChartsData', {'host' : host,'startTime' : startTime,'endTime' : endTime}, function(response){
console.log("viewCharts", response);
obj = response;
//bootbox.alert(obj.status, obj.label);
if (obj.status == 'OK') {
$('.one_half').show();
showMonitoringServer(host, obj);
} else {
show_message('Show Chart Error -' +host, 'Unexpected error occured! Please try again later!', 'error' );
}
}, "json");
});
Have you tried the onChange event?
$('#dateRangeSelector').daterangepicker({
onChange: function(){
//do ajax stuff
}
);
There seems to be a known issue with onChange getting called twice. If it hasn't been fixed in your version, you can always use:
$('#dateRangeSelector').on('blur', function(){
//do ajax stuff
});
I added a callback function for onChange now that you might be able use. Keep in mind that it may not fit your needs exactly because it will fire on EVERY change that occurs in the input. One-click range shortcuts will actually fire 2 change callbacks because this plugin simply triggers date changes on each datepicker, one at a time. Once this plugin is adopted by jQuery UI, I’m sure we’ll work that part out and have the rangepicker firing events as one widget. For now, you’ll need to set up your page with this issue in mind.
Try this.
$(document).ready(function() {
$('#dateRangeSelector').change(function(){
$.get(SITE_URL + 'admin/SystemMonitor/charts/getChartsData', {'host' : host,'startTime' : startTime,'endTime' : endTime}, function(response){
console.log("viewCharts", response);
obj = response;
//bootbox.alert(obj.status, obj.label);
if (obj.status == 'OK') {
$('.one_half').show();
showMonitoringServer(host, obj);
} else {
show_message('Show Chart Error -' +host, 'Unexpected error occured! Please try again later!', 'error' );
}
}, "json");
});
});
You can use
$( "#dateRangeSelector" ).change(function() {
alert( "Handler for .change() called." );
});
You can also have a look at the Jquery API: http://api.jquery.com/change/
From the documentation itself shows:
Follow this link of documentation
$('#dp5').datepicker().on('changeDate', function(ev){
if (ev.date.valueOf() < startDate.valueOf()){
....
}
});
so you need to do this way:
$('#dateRangeSelector').datepicker().on('changeDate', function(ev){
$.get(SITE_URL + 'admin/SystemMonitor/charts/getChartsData', {'host' : host,'startTime' : startTime,'endTime' : endTime}, function(response){
console.log("viewCharts", response);
obj = response;
//bootbox.alert(obj.status, obj.label);
if (obj.status == 'OK') {
$('.one_half').show();
showMonitoringServer(host, obj);
} else {
show_message('Show Chart Error -' +host, 'Unexpected error occured! Please try again later!', 'error' );
}
}, "json");
});

Javascript/jQuery, if and else statements both being executed

I'm not too sure what's going on here and feel I may be missing something that is probably quite obvious, but I have an if else in which both statements are being called. If someone could shed some light on this that would be great. The code below is using asual and should be detecting whether or not a div $('#' + $.address.hash() has already been loaded. However both if and else events are being fired in order. In fact the else statement fires twice...
$('#lmit_back').show(400, function() {
if($('#' + $.address.hash() ).length == 0) {
$('#init').load('inithandler.php', { 'address' : $.address.hash() }, function() {
$('#' + $.address.hash()).fadeIn(1000, function() {
$('#remn').load('pagehandler.php',{ 'address' : $.address.hash() });
});
});
}
else {
alert('hey');
}
});
Here is all the code..I can't really work out what could cause it to execute twice, unless it has something to do with address.change which im not as familiar with as I would like to be.
if (($.address.hash() == '')) {$.address.hash('home');}
$('#lmit_back').click(function() {
$.address.hash('home');
});
$.address.change( function() {
if (!($.address.hash() == 'home'))
{
var exception = '.' + $('[href="#' + $.address.hash() + '"]').parent().attr('class');
$('#left_menu_bar li:not(' + exception + ')').hide(300,function() {
$(exception).show( function() {
$('#left_menu_bar').animate({ width:'100%'},400, function() {
$('#lmit_back').show(400, function() {
if ($('#' + $.address.hash() ).length === 0)
{
$('#init').load('inithandler.php', { 'address' : $.address.hash() } , function() {
$('#' + $.address.hash()).fadeIn(1000, function() {
$('#remn').load('pagehandler.php',{ 'address' : $.address.hash() });
});
});
}
else
{
alert('nigs');
}
});
});
});
});
}
else
{
$('#left_menu_bar').animate({ width:'251px'}, function() {
$('#left_menu_bar li').show( function() {
$('#lmit_back').hide();
});
});
$('#left_menu_bar').css('width','251px');
}
});
The problem here is not arising from the code you have pasted. The only way the code could be running multiple times and hitting multiple branches is if it is being executed more than once. Look to the surrounding code for places where this could be called more than once.
I faced similar issue, while debugging when if condition was true it also went in else block. Later I added alert and console.log in if/else block's & realized, else was not actually being executed but while debugging it looked like it was in else.
So, whoever faces same issue verify by adding alert or console.log.
I was seeing the same behavior, when I debugged step by step it worked. It ended up being that I was attaching multiple click events so the code fired n times.

Categories