Strange JavaScript warning in VS IDE - javascript

I'm getting an odd code syntax warning in Visual Studio, for a bit of JavaScript:
If(1==1) {
alert("!");
}
The warning is on the opening curly brace:
TS1005 (JS) ';' expected.
I tried adding a semicolon after the closing curly brace, but that didn't make a difference:
I've also determined that when this if block is included anywhere in the script—with or without the trailing semicolon—the entire script fails to execute in the browser (Chrome, latest version).
According to W3Schools, the syntax is correct.
This seems very strange. What's going wrong here?

If should not have a capital letter. If is recognized as a function by VS and therefore expects a ;. The warning should be fixed by using if instead of If. I tested it locally on my VS IDE and using If generated the same warning, but if is fine.

Related

How might the characters ​ can appear in the served HTML but not in my source code?

My source code is:
//this is a helper function know as an Immediately Invoked Function Expression -
//use this to fix a closure within a loop
function iife(j){
return function () {
return j;​
} ();
}
But when this is served to the browser, I get Uncaught SyntaxError: Unexpected token ILLEGAL pointing to
function iife(j){
return function () {
return j;​
} ();
}
I'm using a Node.js server which does no pre-processing on the HTML. It's just straightforward serving. I've been using this server for months and have had no unexpected results.
As an additional interesting detail, while there seem to be no spaces after the code in question in my source, my IDE, Aptana Studio, is having trouble highlighting text now. If I delete that code, or even copy and re-paste it, the highlighting goes back to normal for a while. I've even tried re-typing that line from scratch but the problem persists.
Where might be the cause of these ​ characters?
This is a character encoding issue. The editor displaying your source code can handle it just fine, but the JavaScript interpreter cannot.
Make sure your editor is configured for UTF-8 encoding.
These types of encoding issues often arise when "fancy" versions of punctuation marks like slanted quotes are used instead of the standard ones.

How to keep "*/" in regular expression from being interpreted as block comment?

I'm working on a bookmarklet which is using the replaceText plugin to wrap all words (and extraneous spaces/punctuation) on a page in span tags. That plugin traverses all the text nodes on a page and allows me to call a function to manipulate the contents of each one without breaking any other HTML formatting on the page. (None of this is the problem, I'm pretty sure, but I felt like the context might be useful). My call of the function looks like this, for your reference:
$("body *").replaceText(/\S+\s*/g, spanWrap);
The problem is that the best regular expression I've found for separating these words for my purposes -- /\S+\s*/g -- contains the characters for the end of a block comment ("*/"). If I add the opening of a block comment a few lines before it in the .js file in Notepad++, I can see that the syntax highlighter is reading it as that.
When I run my bookmarklet, most sites seem to have no problem with this issue and the bookmarklet works as intended. However, some sites, for reasons I can't predict, throw up an "Uncaught SyntaxError: Unexpected token <" error and the bookmarklet breaks/stops running. If I change the regular expression I'm using in the replaceText function to one I had been using in an earlier version of the bookmarklet -- /\b(\S+?)\b/g -- while changing absolutely nothing else in the bookmarklet, these sites stop giving the error and the bookmarklet works just fine, so I have to believe that it's the presence of the block comment closure that's causing it.
For the purposes of what I'm trying to do with the bookmarklet, though, the expression with that comment closure in it --/\S+\s*/g-- works much, much better than the other one, which doesn't catch punctuation and white space. However, I'd also really like it if my bookmarklet didn't break on certain sites.
So, is there either a way to fix the regular expression that I have so that it's not being read as a comment or can you suggest one that can do the same job maybe with a different syntax or something? (If it's not obvious from my question, I have the barest understanding of how regular expressions work and have gotten the ones I'm using in this example by copying them from other Stack Overflow questions/answers)
Use the long version:
var regex = new RegExp("\\S+\\s*", "g");
$("body *").replaceText(regex, spanWrap);
(EDIT: Escaped the backslashes in the string)
So, is there either a way to fix the regular expression that I have so that it's not being read as a comment
I can't think of anything sane. (You could get the effect by using the RegExp constructor and breaking the regex up into two strings and then concatenating them back together for the regex. I wouldn't call that sane though.)
I'd use a series of line comments // instead of a block comment.

Automatically Fix "missing semicolon before statement"

I have a large javascript file that is missing a lot of semicolons. It works fine as it is, but when I try to use a minifier = kaboom! Is there a way to automatically fix this? I tried to go manually through it but it's not humanly possible.
check out the JavaScript Lint application that checks your script for common errors (semicolon, curly brackets, trailing decimals, nested comments, etc.)

How to solve error: "undefined is null or not object ie in ext js" in IE?

I think its the cause of trailing comma, or syntax error, variable used without declaration. My js fiel is 1000 lines od code. Since the error is not provding me the line no. Its becoming dfficult to debug. Please help me with debugging techniques for IE. The script works very well with Firefox, Safari.
I'd jslint the file. That will find the issue as well as any others you may have.
You can run it as a command line utility via node.
include this <script type="text/javascript" src="https://getfirebug.com/firebug-lite-debug.js"></script> and <html debug="true"> will give you an firebug console
http://getfirebug.com/firebuglite#Debug
For debugging in IE I would recommend you install DebugBar. This extension is similar to FireBug for Firefox.
If you are developing through Microsoft Visual Studio I remember it will help you find trailing commas by highlighting the following } element with a green curly underline.
If you use the built-in developer tools in IE8 and later, you can step through your code in the browser and determine which line causes the error - starting from the top.
If you are not using any debugging tools in IE, then I will advise you to - just like Johan and bjornd are suggesting.
Happy hunting :)
If you are using eclipse :
configure spket plug-in editor for java script
It will highlight the missing/incomplete syntax (e.g comma/semicolon)
so you don't need to debug for syntax errors
Guys, I finally did it ? I took the strength of all your techniques.
1) I put the code in a single
try{
code
}
and catch(e) {
alert('Final Err: '+ e.description);
}
and kept initial 200 lines uncommented and the rest commented and ran the file
while(EOF) {
if(got an alert of error)
checked for trailing commas & putting missed semicolons till end.
else
adding some more lines later uncommented out of the commented and ran the file.
}
Finally, the page got succesfully loaded !!!
I had this issue using the Extjs RowExpander user extension. The issue only occurred in IE. I was able to fix it by adding a few lines of code at the top of the 'toggleRow' method:
if (!this.view) {
this.bindView();
}
For some reason IE occasionally chokes on references to 'this.view' (likely a timing issue). Running 'bindview()' ensures that 'this.view' resolves appropriately.

SquishIt javascript error when minimized: 'missing ; before statement'

When I run SquishIt in development mode everything works fine. When I put it into production mode I am getting the 'missing ; before statement' error in Javascript. I've tried removing the offending javascipt files, but the error moves down to the next one.
Any idea's?
One possibility is that you're missing a ; somewhere in your code that's being automatically inserted by the browser. Use jslint to find the offending lines of code.
I also posted this problem on the Squishit google groups. Justin reponded:
This is a known bug that is going to
be fixed in an soon to be released
SquishIt 0.6.1. Essentially the ajax
minifier is compressing the files
separately before we are combining
them into one. This can cause the
issue you are seeing. The fix in the
meantime is to use the WithCompressor
option and switch to something like
the YUI compressor temporarily.the YUI compressor temporarily.
Bundle.JavaScript()
.Add("/Scripts/Frameworks/jquery.js").WithMinifier(JavaScriptMinifiers.Yui)
....

Categories

Resources