Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Managing Client-side Scripts in the IDE

0.00/5 (No votes)
10 Mar 2004 1  
Manage Client-side ccripts in the IDE.

Introduction

Managing client-side VBScripts is often necessary in some projects. Here is a solution how to do it. All VBScripts are held in a special project folder. The script is loaded into a hidden textbox and executed via JavaScript.

Sample screenshot

Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click
  Dim sText As String = "Hello!"
  ClientBrowserVBScript("", "msgbox(" & Chr(34) & sText & Chr(34) & ")")

  'Pass addidional Variables

  Dim sAdd1 As String = "sServerName=" & _
           Chr(34) & Server.MachineName & Chr(34)
  ClientBrowserVBScript("SayHello.vbs", sAdd1)
End Sub
 
Sub ClientBrowserVBScript(ByVal sVBScriptName As String,_
         Optional ByVal sAddScript As String = "")
  Dim sc As String = Replace(sVBScriptName, ".", "")
  Dim sb As New System.Text.StringBuilder
  
  'Create a hidden Textarea as Script-Container

  sb.Append("<div style='position:" & _
       "absolute;top:100;left:100;visibility:hidden;'>")
  sb.Append("<textarea name='ClientBrowserVBScript" &_
     sc & "' id='ClientBrowserVBScript" & _
     sc & "' cols='100' rows='10'>" & vbCrLf)
  sb.Append("On Error Resume Next" & vbCrLf)
  sb.Append(sAddScript & vbCrLf)
  
  'Append the VBScript from the Project

  If sVBScriptName <> "" Then
    Dim fs As New StreamReader(Server.MapPath("ClientScripts/" _
                                                  & sVBScriptName))
    sb.Append(fs.ReadToEnd)
    fs.Close()
  End If

  sb.Append("</textarea></div>" & vbCrLf)
  'Suppress Javascript-Error

  sb.Append("<script>" & vbCrLf)
  sb.Append("onerror = stopError;" & vbCrLf)
  sb.Append("function stopError()" & vbCrLf)
  sb.Append("{" & vbCrLf)
  sb.Append("return true;" & vbCrLf)
  sb.Append("}" & vbCrLf)

  'Extract the Script

  sb.Append("sScript = window.document.getElementById(" _
      & Chr(34) & "ClientBrowserVBScript" & _
      sc & Chr(34) & ").value;" & vbCrLf)

  'Execute VBScript under Javascript 

  sb.Append("javascript:window.execScript(sScript, " & _
              Chr(34) & "VBScript" & Chr(34) & ");")
  sb.Append("</script>")
  Page.RegisterClientScriptBlock("ASPXClientBrowserVBScript" _
                                                  & sc, sb.ToString)
End Sub

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here