使用Vbscript进行输入校正

时间:2015-02-13 04:04:16

标签: vbscript hta

我的Hta使用vbscript添加书签。当用户键入http://www.Google.com/等网址时效果很好但是当用户只输入www.Google.com时,它会添加一个按钮,但此时按钮不起作用,最后显示无效地址错误。代码 -

      <HTML xmlns:IE>
      <HEAD>

       <TITLE>Bookmarks</TITLE>

     <HTA:APPLICATION 
      ID="appbook"
      VERSION="1.0"
     APPLICATIONNAME="Bookmarks"
      SYSMENU="yes"
MAXIMIZEBUTTON="Yes"
MINIMIZEBUTTON="yes"
BORDER="thin"
    ICON="img\img.icoh"
INNERBORDER="thin"
SCROLL="Yes"
    SINGLEINSTANCE="no"
WINDOWSTATE="Maximize"
    CONTEXTMENU="NO"
     >
    <BODY>
    <SCRIPT LANGUAGE="VBScript">

    Sub Window_OnLoad
      window.offscreenBuffering = True
  Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objFile = objFSO.OpenTextFile("windowssettinguser.ini", 1)
     strContents = objFile.ReadAll
   objFile.Close

    strHTML = UserArea.innerHTML
    strHTML = strContents
    UserArea.innerhtml = strhtml
    end sub

    sub addlink1
    firstresponse = inputbox("Please Enter Web Address Of Your Favourite Web Page Or Item. NOTE THAT - Use ''http://'' In Front Of Your Web Adress Either You Will Be Dealing With A Error." ,"Add New Address  ")
    if firstresponse = "" then
        alert "enter something"
     else
       secondresponse = inputbox("Please Enter Name Of Your Desire Which Replace 'Your Link Here' In Main Window.","LinkzMe - Edit Button")
      if secondresponse = "" then
      alert "Enter something"
     else

      Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objFile = objFSO.OpenTextFile("windowssettinguser.ini", 2)
      objFile.Writeline "<input type=" & chr(34) & "button" & chr(34) & "class=" & chr(34) & "button" & chr(34) & "value=" & chr(34) & secondresponse & chr(34) & "onclick=" & chr(34) & "window.location.href="& chr(39) & firstresponse & chr(39) & chr(34)  & "STYLE=" & chr(34) & "position: absolute; right: 365 ; top: 156;" & chr(34) & ">"            objFile.Close
     Window_OnLoad
      Msgbox "Bookmark Added Successfully.","0","Job Done"
     end if 
     end if
      end sub
     </script>
       <input type="button" class="button" value="Add Bookmark" name="addlink1" onClick="addlink1"  >
     <span id = "UserArea"></span>
     </BODY>

1 个答案:

答案 0 :(得分:1)

我做了一些修改,比如检查文件 windowssettinguser.ini 是否存在;如果不存在,则以附加模式创建它。

如果用户输入的网址不包括在内,则添加协议Http。

<HTML>
<HEAD>
<TITLE>Bookmarks</TITLE>
<HTA:APPLICATION
ID="appbook"
VERSION="1.0"
APPLICATIONNAME="Bookmarks"
SYSMENU="yes"
MAXIMIZEBUTTON="Yes"
MINIMIZEBUTTON="yes"
BORDER="thin"
ICON="magnify.exe"
INNERBORDER="thin"
SCROLL="Yes"
SINGLEINSTANCE="no"
WINDOWSTATE="Maximize"
CONTEXTMENU="NO"
>
<style>
body{
background-color: DarkOrange;
}
</style>
<META HTTP-EQUIV="MSThemeCompatible" CONTENT="YES">
<BODY>
<SCRIPT LANGUAGE="VBScript">

Sub Window_OnLoad
    window.offscreenBuffering = True
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists("windowssettinguser.ini") Then
        Set objFile = objFSO.OpenTextFile("windowssettinguser.ini",1)
        strContents = objFile.ReadAll
        objFile.Close
        strHTML = UserArea.innerHTML
        strHTML = strContents
        UserArea.innerhtml = strhtml
    else
        Set objFile = objFSO.OpenTextFile("windowssettinguser.ini",8,True)
    End If
end sub

sub addlink1
    Title="Add Web Address Of Your Favourite Web Page"
    firstresponse = inputbox("Please Enter Web Address Of Your Favourite Web Page Or Item !",Title)
    if firstresponse = "" then
        alert "enter something"
    else
        secondresponse = inputbox("Please Enter Name Of Your Desire Which Replace 'Your Link Here' In Main Window.","LinkzMe - Edit Button")
        if secondresponse = "" then
            alert "Enter something"
        else
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            Set objFile = objFSO.OpenTextFile("windowssettinguser.ini",8)
            ProtocoleHTTP = "http://"
            If Left(firstresponse,7) <> ProtocoleHTTP Then
                firstresponse = ProtocoleHTTP & firstresponse
            End if
            objFile.Writeline "<hr><input type=" & chr(34) & "button" & chr(34) & "class=" & chr(34) & "button" & chr(34) & "value=" & chr(34) & secondresponse & chr(34) & "onclick=" & chr(34) & "window.location.href="& chr(39) & firstresponse & chr(39) & chr(34) & "Title="& firstresponse &">"
            objFile.Close
            Msgbox "Bookmark Added Successfully.",Vbinformation,"Job Done"
            window.location.reload(True)
        end if
    end if
end sub
</script>
<input type="button" class="button" value="Add Bookmark" name="addlink1" onClick="addlink1"  >
<span id = "UserArea"></span>
</BODY>
</html>
相关问题