简体   繁体   中英

Javascript missing semicolon, where?

I am using vb.net with javascript. Whenever I click a link on page 1, it loads and opens page 2. As part of this, in the page_load I have a bit of code which registers a javascript function as below:

   Private Sub regjs()
        Dim requesttype = "xx"
        Dim url As String = "page.aspx?rtype=" & requesttype & "&claimid="

        Dim s As New StringBuilder
        s.Append("<script type=""text/javaScript"">")
        s.Append("var r = new Object();" & ControlChars.CrLf)
        s.Append("if (window.returnValue == 'undefined') { window.returnValue = ''; }" & ControlChars.CrLf)
        s.Append("function vt(param) { " & ControlChars.CrLf)
        s.Append("if (param !== null) {" & ControlChars.CrLf)
        ***s.Append("var jurl = '" & url & "'")
        s.Append("param;" & ControlChars.CrLf)***
        s.Append("var r = window.showModalDialog(jurl,'','dialogWidth:500px;dialogHeight:500px;resizable:no');" & ControlChars.CrLf)
        s.Append("}" & ControlChars.CrLf)
        s.Append("}" & ControlChars.CrLf)
        s.Append("</script>")

        If Not ClientScript.IsClientScriptBlockRegistered("js") Then
            ClientScript.RegisterClientScriptBlock(Me.GetType(), "js", s.ToString())
        End If
    End Sub

I am getting an error saying there is a missing semicolon when the page attempts to load. The error is occuring on the s.Append("param;" * ControlChars.CrLf) line saying there is a missing ";". Can anyone point out where I have gone wrong? (I am trying to construct a url with a variable as a query string value).

Thanks, C

That code outputs the following string:

<script type="text/javaScript">var r = new Object();
if (window.returnValue == 'undefined') { window.returnValue = ''; }
function vt(param) { 
if (param !== null) {
var jurl = 'page.aspx?rtype=xx&claimid='param;
var r = window.showModalDialog(jurl,'','dialogWidth:500px;dialogHeight:500px;resizable:no');
}
}
</script>

As you can see, this line is invalid:

var jurl = 'page.aspx?rtype=xx&claimid='param;

I suspect that you intended to do a string concatenation there.

Also, it's worth mentioning that you should just use StringBuilder.AppendLine rather than Append with ControlChars.CrLf .

声明变量时,需要以分号结束语句。

s.Append("var jurl = '" & url & "'")
s.Append("param;" & ControlChars.CrLf)

builds the code

var jurl = 'page.aspx?rtype=xx&claimId='param;
                                       ^

The string (and the statement) ends at the indicated point, but there is no semicolon.

One assumes you meant to add the param to the end of the string, in which case you are missing a + .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM