Google closure compiler javascript parse error - javascript

I am trying to use google closure compiler on my javascript files. It works fine except for the following piece of code:
function goto(form) { var index=form.select.selectedIndex
if (form.select.options[index].value != "0") {
location=form.select.options[index].value;}}
The compiler returns:
JSC_PARSE_ERROR: Parse error. missing ( before function parameters. at line 1 character 9
function goto(form) { var index=form.select.selectedIndex
There is a caret (^) pointing to the g in "goto" when it is output on the screen.
I am using just the basic UI version here to test:
http://closure-compiler.appspot.com/home
Any idea what is wrong with the javacript? It seems to work just fine but I am not a javascript person so I have no clue how to fix it. Thanks,
Bill

I am not sure why #Sirko deleted his answer. So I will add it.
You need to change the name of the function goto to something else. Something like gotoUrl, gotoPage, etc.
It was a reserved word in ECMAScript 3, but removed in ECMAScript 5. I am guessing the closure compiler uses that older list still.

Related

JavaScript Object Property Includes SPACE Character

I'm working in VS Code on a VueJS 3 project. I have Vetur Extension (version: v0.35.0) installed for error checking and syntax highlighting. I'm getting notified by red tick mark and output in the Problems window about the format of how I'm identifying Object Properties that include a space character in their definition. Here is an example:
this.ncCases = this.store.state.csvJson.data.filter(obj => {
return obj.[`Neighborhood Council`] == this.selectedNC
})
The property "Neighborhood Council" includes a space. I found a reference at mozilla.org that describes enclosing these properties within Square Brackets. However, when I do this, I get the Red Error Market of death.
Identifier expected. Vetur(1003)
Must say the code runs without error so I think the problem is Vetur Extension does not recognize this syntax. Never the less, it would be nice to fix the code to not generate the error or fix Vetur to not display a false positive error.
Any suggestions?
this.ncCases = this.store.state.csvJson.data.filter(obj => {
return obj['Neighborhood Council'] == this.selectedNC
})
This should be fine in order to get access to the property
Maybe this response could help you in order to disable some checks here

Changing indentation style in atom editor (javascript code)

I've used microsoft's vscode for some months, but since 2 updates the indentation is broken for me, without a way to fix it. I therefore consider switching to atom. I was working with atom before, and never had this problem, but now it seems to have the same (wrong for me) behaviour as vscode:
if (xxx) // press enter here and type 'something();'
should result in
if (xxx)
something();
but it results in
if (xxx)
something();
It will work completely finde if you add { }, but without them its like the above.
How can I fix this in atom? The basic identation settings don't seem to cover this.
My way of doing this si to always use {} (also because I'm using linter-standard-js) That way, you type
if (myCondition) {}
When you return between the accolades, you get the following
if (myCondition) {
// indented and here you go
}
You can add extra conditions for Atom to change the indent level in your config.cson file, following the lead of the packages that define those conditions in the first place. Add the following to the top level of config.cson and Atom will automatically add a level of indentation after a line that matches the regular expression if\s*\(.*\)$. If you already have a .source.js entry, make sure to change that instead of just pasting this bit in.
'.source.js':
editor:
increaseIndentPattern: '(?x)
\\{ [^}"\']* $
| \\[ [^\\]"\']* $
| \\( [^)"\']* $
| if\\s*\\(.*\\)$
'

Does this kind of value setting work in Javascript?

I'm trying to create code that requires the least number of bytes and that works for all browsers including IE 7.
In this example, the program calls dosomething('x1') and dosomething('x2').
If I have code like this:
var items,item,index,count;
items=Array('x1','x2');
count=items.length;
for (index=0;index<count;index++){
item=items[index];
dosomething(item);
}
Could I reduce it to this and have it still function exactly the same in all browsers:
var a=Array('x1','x2'),c=a.length,i;
for (i=0;i<c;i++){
f(a[i]);
}
I understand I changed the variable names and calling function name but my goal is to use the least number of bytes possible in the code to make the code execute.
I'm just not sure if declaring a variable equal to a property of a value from a previous variable in the same list of declarations would actually return correct results.
In other words, does var a=Array('x1','x2'),c=a.length... work, or do I have to specifically do var a=Array('x1','x2');var c=a.length; to make it work in all browsers including IE 7?
This is what the Google Closure Compiler service returned:
var a,b,c,d;a=["x1","x2"];d=a.length;for(c=0​;c<d;c++)b=a[c],dosomething(b);
You can find many different Javascript compressors online to automate the process you are hand coding now. Yet, it's always good to understand how they work as it helps to write code that is better compressed.
As for IE, you can test your code by changing the emulations settings in the IE debugger panel. Just press F12, click the Emulation tab, and adjust the document mode to 7 (IE7).
Hope this is enough to get you started in the right direction.
You can use Array.map from IE 9
var items = Array('x1','x2');
items.map(dosomething(item));

How to make JSLint tolerate a leading semi-colon?

The continuous integration software I am using runs JavaScript files through JSLint and complains if they fail (using default JSLint settings it appears).
I've always prepended a ; to the start of my jQuery plugins, so concatenating them doesn't lead to something like this...
common.js
I don't have access to this file, and can't enforce a semi colon at the end.
var abc = function() {
alert(arguments[0]);
}
plugin.js
This is my file that is concatenated with common.js. It is appended straight to the end of common.js.
(function($) {
$.fn.somePlugin = function() {}
})(jQuery);
jsFiddle of the problem this can cause.
jsFiddle of the solution (leading semi-colon of my plugin).
However, JSLint complains with...
Error:
Problem at line 1 character 1: Unexpected space between
'(begin)' and ';'.
;(function($) {
Problem at line 1 character 1: Expected ';' at column 5, not column 1.
;(function($) {
Problem at line 1 character 2: Missing space between ';' and '('.
...
I tried using a bang operator (!) instead, and a few other alternatives, but JSLint still complained.
What can I do to get this safety net and pass JSLint?
Patch JSLint to not mind. Everyone will benefit.
Possibly a flippant answer, but your html can probably do this:
<script src="common.js"></script>
<script>;</script>
<script src="plugin.js"></script>
Or if you don't like the inline script on the second line, make a file called semicolon.js and, well, you know....
Sorry if this is ridiculous, but JSLint does not like a plain old empty statement on a line by itself. Somewhat unfortunate. Oh well.
BTW awesome fiddle. Seeing that alert run because of the missing semicolon after the var declaration of the function was, like, wow what do you know? That function got called as it should! Very interesting.
What happens if you put the ; on a line by itself, or put some other syntactically valid but meaningless statement like this:
"begin plugin";
(function() { // etc
EDIT: what about using void:
void("begin plugin");
(function() { // etc
//or
void(
(function() { /* your code */ })()
);
EDIT 2: what about some variation on this:
if (console && console.log) { // or if (false)?
console.log("Begining plugin definition");
}
(function { // etc

how to fix javascript error in ie7/8?

if you open the page:
http://www.rhino.com/shop/product/the-monkees-the-birds-the-bees-the-monkees-boxed-set
you will see at the bottom of the page, a next and previous buttons, these are navigation buttons, they work fine in firefox but in ie the style is removed from the page when they are clicked!. please advise on what might be the cause of the problem. i have already spent hours debugging :(
When I debug it on IE8, I see a runtime error here:
cd=new Date();
Most likely this does not accord with this piece of HTML:
<li id="cd" class="">CD</li>
IE 'helpfully' launches elements with an id as global variables. So cd is actually a <li> element, and IE doesn't like it if you assign new Date() to it. It would be perfectly fine if IE would treat cd as a normal local variable, but it does not, hence the runtime error.
This piece of code is inside a anonymous function which is really nasty (shown below). But the solution is simple: just write proper functions, and declare your variables. If the anonymous function would have used one line in the top declaring variables like this:
var cd, dc, ...other variable names... ;
it would have worked just fine, because then the cd variable inside the function would have referred to the local variable, not the global cd element which IE thinks referes to the li element with id="cd".
Now it is entirely possible that this is just one of many problems with this page. But I am assuming the first error encountered by IE stopped execution of the remainder of your script, which why some of it diddn't work.
function anonymous(t, z, y) {
dc=new Date('1/1/2000');f=15;ne=8;
if(dc.getDay()!=6||dc.getMonth()!=0){
return'Data Not Available'
}else{;
z=parseInt(z);
if(y=='2009'){f=8;ne=1};
gmar=new Date('3/1/'+y);
dsts=f-gmar.getDay();
gnov=new Date('11/1/'+y);
dste=ne-gnov.getDay();
spr=new Date('3/'+dsts+'/'+y);
fl=new Date('11/'+dste+'/'+y);
cd=new Date();
if(cd>spr&&cd<fl){
z=z+1
}else{
z=z
};
utc=cd.getTime()+(cd.getTimezoneOffset()*60000);
tz=new Date(utc + (3600000*z));
thisy=tz.getFullYear();
var days=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
if(thisy!=y){
return'Data Not Available'
}else{;
thish=tz.getHours();
thismin=tz.getMinutes();
thisd=tz.getDay();
var dow=days[thisd];var ap='AM';var dt='Weekday';
var mint='00';
if(thismin>30){mint='30'}
if(thish>=12){ap='PM';thish=thish-12};
if (thish==0){thish=12};
if(thisd==6||thisd==0){dt='Weekend'};
var timestring=thish+':'+mint+ap;
var daystring=dow;
var endstring=dt;
if(t=='h'){
return timestring}
if(t=='d'){
return daystring};if(t=='w'){return endstring}}};
}
Now debugging where this is comming from is a bit of a prob. My callstack reads:
anonymous JScript
s_doPlugins JScript
anonymous function JScript
global code JScript
The top one is where the actual error occurs.
The root where this is all happening is here:
/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
var s_code=s.t();if(s_code)document.write(s_code)
Somehow, from here, s_doPlugins is called which contains the actual call to the anonymous function that is causing the trouble. It looks like s_doPlugins is located in s_code.js. If I step through that, I find that the trouble is in this line:
s.prop9=s.getTimeParting('h','-5','2008'); // Set hour
which is line 38 of s_code.js. With a little bit more poking around, I find it is actuall this plugin:
/*
* Plugin: getTimeParting 1.3 - Set timeparting values based on time zone
*/
s.getTimeParting=new Function("t","z","y",""
+"dc=new Date('1/1/2000');f=15;ne=8;if(dc.getDay()!=6||"
...more crap here...
This is at line 95 in s_code.js
As a quick fix i would probably comment out all calls to s.getTimeParting() in s_doPlugins() in s_code.js and see if that fixes your problem. Then, the long and hard but noble taks remains of churning something sensible out of this mess :))
I found this infuriating and discovered that the problem was my site using a div with the ID "utc", which clashes with the s_code JS. 'UTC' is time-related so I can only assume that there's sloppy JS relying on it being something else if present at all!
With 2021 soon upon us, I wanted to resurface an issue with older versions of the getTimeParting plug-in that made quite the stir at the beginning of this year. If you took the easy route and edited the plug-in code instead of upgrading to the latest version, you'll want to make sure that you make those same edits again before January 1.
More information on this topic can be found in this forum thread:

Categories