Click here to Skip to main content
15,881,620 members
Articles / Web Development / ASP.NET
Article

Managing Client-side Scripts in the IDE

Rate me:
Please Sign up or sign in to vote.
1.46/5 (11 votes)
10 Mar 2004 33.2K   356   12   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

VB
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


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralASP.Net 2.0 feature: WebResource.axd Pin
Eric Newton6-Nov-04 10:10
Eric Newton6-Nov-04 10:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.