Javascript: missing syntax error} after function body

Ok, so you know the error, but why am I getting it? I don't get any errors when it runs locally, but when I loaded my project, I got this annoying syntax error. I checked the firebug error console, which does not help, because it puts all the source code in one line, and I analyzed it through Lint, which did not seem to find the problem either - I just finished formatting my curly braces otherwise I hate it; on the same line as the statement, bleug.

function ToServer(cmd, data) { var xmlObj = new XMLHttpRequest(); xmlObj.open('POST', 'handler.php', true); xmlObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xmlObj.send(cmd + data); xmlObj.onreadystatechange = function() { if(xmlObj.readyState === 4 && xmlObj.status === 200) { if(cmd == 'cmd=push') { document.getElementById('pushResponse').innerHTML = xmlObj.responseText; } if(cmd == 'cmd=pop') { document.getElementById('messages').innerHTML += xmlObj.responseText; } if(cmd == 'cmd=login') { if(xmlObj.responseText == 'OK') { self.location = 'index.php'; } else { document.getElementById('response').innerHTML = xmlObj.responseText; } } } } } function Login() { // Grab username and password for login var uName = document.getElementById('uNameBox').value; var pWord = document.getElementById('pWordBox').value; ToServer('cmd=login', '&uName=' + uName + '&pWord=' + pWord); } // Start checking of messages every second window.onload = function() { if(getUrlVars()['to'] != null) { setInterval(GetMessages(), 1000); } } function Chat() { // Get username from recipient box var user = document.getElementById('recipient').value; self.location = 'index.php?to=' + user; } function SendMessage() { // Grab message from text box var from = readCookie('privateChat'); var to = getUrlVars()['to']; var msg = document.getElementById('msgBox').value; ToServer('cmd=push','&from=' + from + '&to=' + to + '&msg=' + msg); // Reset the input box document.getElementById('msgBox').value = ""; } function GetMessages() { // Grab account hash from auth cookie var aHash = readCookie('privateChat'); var to = getUrlVars()['to']; ToServer('cmd=pop','&account=' + aHash + '&to=' + to); var textArea = document.getElementById('messages'); textArea.scrollTop = textArea.scrollHeight; } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } function getUrlVars() { var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { vars[key] = value; }); return vars; } 

One gold medal for those who can solve it.

Greetings.

+4
source share
7 answers

The problem is that your script on your server is on the same line and there are comments in it. the code after // will be considered as a comment. This is the reason.

 function ToServer(cmd, data) { var xmlObj = new XMLHttpRequest(); xmlObj.open('POST', 'handler.php', true); xmlObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xmlObj.send(cmd + data); xmlObj.onreadystatechange = function() { if(xmlObj.readyState === 4 && xmlObj.status === 200) { if(cmd == 'cmd=push') { document.getElementById('pushResponse').innerHTML = xmlObj.responseText; } if(cmd == 'cmd=pop') { document.getElementById('messages').innerHTML += xmlObj.responseText; } if(cmd == 'cmd=login') { if(xmlObj.responseText == 'OK') { self.location = 'index.php'; } else { document.getElementById('response').innerHTML = xmlObj.responseText; } } } };}function Login() { // Grab username and password for login var uName = document.getElementById('uNameBox').value; var pWord = document.getElementById('pWordBox').value; ToServer('cmd=login', '&uName=' + uName + '&pWord=' + pWord);}// Start checking of messages every secondwindow.onload = function() { if(getUrlVars()['to'] != null) { setInterval(GetMessages(), 1000); }}function Chat() { // Get username from recipient box var user = document.getElementById('recipient').value; self.location = 'index.php?to=' + user;}function SendMessage() { // Grab message from text box var from = readCookie('privateChat'); var to = getUrlVars()['to']; var msg = document.getElementById('msgBox').value; ToServer('cmd=push','&from=' + from + '&to=' + to + '&msg=' + msg); // Reset the input box document.getElementById('msgBox').value = "";}function GetMessages() { // Grab account hash from auth cookie var aHash = readCookie('privateChat'); var to = getUrlVars()['to']; ToServer('cmd=pop','&account=' + aHash + '&to=' + to); var textArea = document.getElementById('messages'); textArea.scrollTop = textArea.scrollHeight;}function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null;}function getUrlVars() { var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { vars[key] = value; }); return vars;} 
+16
source

You are missing a colon:

 function ToServer(cmd, data) { var xmlObj = new XMLHttpRequest(); xmlObj.open('POST', 'handler.php', true); xmlObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xmlObj.send(cmd + data); xmlObj.onreadystatechange = function() { if(xmlObj.readyState === 4 && xmlObj.status === 200) { if(cmd == 'cmd=push') { document.getElementById('pushResponse').innerHTML = xmlObj.responseText; } if(cmd == 'cmd=pop') { document.getElementById('messages').innerHTML += xmlObj.responseText; } if(cmd == 'cmd=login') { if(xmlObj.responseText == 'OK') { self.location = 'index.php'; } else { document.getElementById('response').innerHTML = xmlObj.responseText; } } } }; //<-- Love the semi } 

Additional missing semicolon:

 // Start checking of messages every second window.onload = function() { if (getUrlVars()['to'] != null) { setInterval(GetMessages(), 1000); } }; //<-- Love this semi too! 
+2
source

I think you can adapt divide and rule here. Delete the last half of your script and see if the error goes if you do not delete the first part and see. This is the method that I follow when I get this kind of problem. Once you find the half with the error, then divide that half further until you indicate the location of the error.

This will help us determine the actual error.

I do not see any problems with this script.

It may not be the exact solution you want, but a way to find and fix your problem.

+2
source

enter image description here

This seems to be interpreted as all on one line. See the same results in the fiddler2 file.

+2
source

It looks like the following code also needs to add another semi

 // Start checking of messages every second window.onload = function() { if(getUrlVars()['to'] != null) { setInterval(GetMessages(), 1000); } }; <---- Semi added 

Also here, in this code, define the top of the var function

 function readCookie(name) { var i; var nameEQ = name + "="; var ca = document.cookie.split(';'); for(i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } 

Hope this helps you

+1
source

"I think I found the key ... I use notepad ++ and until recently I used my cpanel file manager to upload my files. Everything was fine until I used the FireZilla FTP client. I assume the FTP client changes the format or encoding of my JS and PHP files. - "

I believe this was your problem (you probably solved it already). I just tried another FTP client after working with this stupid error, and it worked flawlessly. I assume that the code I used (which was written by another developer) also does not close the comments correctly.

0
source

This problem may occur because your JS code contains minimal comments. If yes, and you want to leave your comments, try changing your comments - for example, from this:

// Reset the input box

... before...

/* Reset the input box */

0
source

All Articles