将变量名添加到HTML标记中

Add variable name to HTML Markups

本文关键字:HTML 变量名 添加      更新时间:2023-09-26

下面的VBScript使用来自输入对话框的输入生成两个文件。下面我想将文件名(fileName)添加到以下脚本创建的JavaScript文件中的document.write('Confirm file"&filename&" completed');中。

但是当我这样做时,它会像上面写的那样以引号括起来的变量名返回,而不是变量本身。我知道这和引言有关,但我不知道该怎么做。任何帮助都会很感激。

fileName=InputBox("Enter a Name for the new SubRoutine: ","Enter    Value","NewRoutine")
CreateFolder
CreateHTML
CreateJS
Sub CreateHTML
  Set objFS = CreateObject("Scripting.FileSystemObject")
  Set objNewFile = objFS.CreateTextFile(fileName&"'"&fileName&".html")
  objNewFile.WriteLine "<html>"
  objNewFile.WriteLine "<head>"
  objNewFile.WriteLine "<title>Created by Robot--MM</title>"
  objNewFile.WriteLine  "</head><body>"
  objNewFile.WriteLine "<script type='text/javascript'     src='"&fileName&".js'></script>"
  objNewFile.WriteLine "<script>"&fileName&"()</script>"
  objNewFile.WriteLine "</body>"
  objNewFile.WriteLine "</html>"
  objNewFile.Close
End Sub
Sub CreateJS
  Set objFS = CreateObject("Scripting.FileSystemObject")
  fileName2 = fileName & ".js"
  Set objNewFilejs = objFS.CreateTextFile(fileName&"'"&fileName&".js")
  objNewFilejs.WriteLine "var "&fileName&" = function() { window.alert('Hi,This is an alert from the file : "&fileName2&"');"
  objNewFilejs.WriteLine "document.write('Confirmed file loaded.'); }"
  objNewFilejs.Close
End Sub
Sub CreateFolder
  Set objShell = CreateObject("Wscript.Shell")
  strPath = Wscript.ScriptFullName
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFSO.GetFile(strPath)
  strFolder = objFSO.GetParentFolderName(objFile)
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFSO.CreateFolder(strFolder & "/"&fileName)
  objShell.Run objFolder
  Set Sh = Nothing 
End Sub

我做了一个简化的回答。我意识到我把引语放错地方了。这里有一些升级的解决方案。

    fileName = InputBox("Please enter a name for your new files:", _"Create File")
    US =                CreateObject("WScript.Shell").ExpandEnvironmentStrings("%UserProfile%")
   Sub CreateHTML
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objNewFile = objFS.CreateTextFile(US &"'Desktop'"& fileName&".html")
    objNewFile.WriteLine "<html>"
    objNewFile.WriteLine"<link rel = 'stylesheet' href='"&fileName&".css'>"
    objNewFile.WriteLine "<head>"
    objNewFile.WriteLine "<title>Created by Robot--MM</title>"
    objNewFile.WriteLine  "</head><body>"
    objNewFile.WriteLine "<script type='text/javascript'       src='"&fileName&".js'></script>"
    objNewFile.WriteLine "<script> "&fileName&"()""</script>"
    objNewFile.WriteLine "</body>"
    objNewFile.WriteLine "</html>"
    objNewFile.Close
    End Sub

    CreateHTML 

    Sub CreateJS
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objNewFilejs = objFS.CreateTextFile(US &"'Desktop'"& fileName&".js")
    objNewFilejs.WriteLine "window.alert('Hi,This is an alert from the file ');"
    objNewFilejs.WriteLine "document.write('Document loaded')"
    objNewFilejs.Close
    End Sub

    CreateJS

    Sub CreateCSS
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objNewFileCSS = objFS.CreateTextFile(US &"'Desktop'"& fileName&".css")
    objNewFilecss.WriteLine "body {text-align:center;font-family:'Zapfino',serif;}"
    objNewFilecss.Close
    End Sub
    CreateCSS