scope - Javascript var hoisting issue -
I have a very simple javascript function that kills MS SQL Server and returns some records. There is a room that I want to display only in the top table row when it is unique. I believe my problem is because the variable which I assigns within the loop does not work because the price is not used to compare it with the previous records. Here is the code
function search index () {var termcounter = ""; Flyout HTML = "& lt; Table & gt; '; Var adOpenDynamic = 2 var adLockOptimistic = 3 var conn = New ActiveXObject ("ADODB.Connection"); Var connectionstring = "Provider = SQLOLEDB; Server = XXXXXXXX; Integrated Security = SSPI; Database = YYYYYYYYYY;" Conn.Open (connectionstring) var rs = New ActiveXObject ("ADODB.Recordset") rs.Open ("SELECT field1, field2, field4, field4, field5 to dbo.table;", conn) if (rseof) {flyoutHTML + '& Lt; Tr & gt; & Lt; Td align = "center" & gt; No Records Found! & Lt; / Td> & Lt; / Tr & gt; '; } Else {while (! Rs.eof) {if (termcounter! = Rs (0)) {var termcounter = rs (0); Flyout HTML + = '& lt; Tr & gt; '; Flyout HTML + = '& lt; Td colspan = "3" & gt; + RS (0) + '& lt; / Td> '; Flyout HTML + = '& lt; / Tr & gt; '; } Flyout HTML + = '& lt; Tr & gt; '; Flyout HTML + = '& lt; Td> + RS (1) + '& lt; / Td> & Lt; Td> + RS (2) + '& lt; / Td> & Lt; Td> + RS (3) + '& lt; / Td> '; Flyout HTML + = '& lt; / Tr & gt; '; Rsmovenext} rs.close conn.close Flyout HTML + = '& lt; / Table & gt; '; }
wordpress = rs (0);
You may be right about hoisting - JavaScript does not have a block space, so both times you are in the same scope. This is a very common mistake because JavaScript looks like C-based languages, which have block scope.
Declaring a var in a conditional or looping block is equivalent to declaring that work at the beginning, in the block, scope-wise.
If you always keep your var declarations at the top of the floor, you will lose less hair - never conditionally or in a loop block. . You can use it to warn you (Oh, by the way, you are missing a ton of semicolons - JSLInd will harm your feelings.)
Why are you repeating it? Skip the second "work" and do the assignments.
Microsoft stuff is foreign to me, but do I have RS (0) syntax Bigging RS an object? Or is there any magic that makes it even in a ceremony (you never know with JS)? And I wonder if you need to use rs.fields (0) or rs.fields (0) .name or rs.fields (0) .value or something.
Comments
Post a Comment