Running vbscript in html file
Solution 1
When you were using the FileSystemObject from an ASP page, then you were manipulating the file-system of the server. This is permitted.
However, when you use the code above, you are executing the code on the client. It is not permitted to access the clients file-system from inside Internet Explorer, as it would have serious security implications. The technical term is "sandboxing".
If you need to interact with the file-system on the client machine, you will need to use a technology such as ActiveX.
Solution 2
When you run the script as client script, it would try to access the file from the client computer, not the server. The file isn't there, and even if it was, your script would not be allowed to access it.
Solution 3
You should consider using HTML Applications by renaming your file with a .hta suffix.
An HTA executes without the constraints of the internet browser security model; in fact, it executes as a "fully trusted" application.
HTML files running inside internet browsers are considered as "untrusted" because the code comes from the internet, and is generally considered as "untrusted" as such the browser enforces a tight security model that prevents those HTML pages gaining access to your computer, which is why the FileSystemObject is unable to open the text file. However, as a HTA it is no longer being run by your browser, but via Microsoft's MSHTA application which grants your script full trust.
For more information see HTML Application - Wikipedia.
Jo Jo
Updated on November 22, 2022Comments
-
Jo Jo about 1 month
I wrote vbscript inside my html file for my site and I can't get it to work. I know it only works in internet explorer as thats the common answer I see people write with this issue. I am able to get basic vbscript working, but when trying to use filesystemobjects to open a text file nothing happens. Code being used is below.
<Script type="text/vbscript"> Dim fsobj, objtxt, thearr Set fsobj = CreateObject("Scripting.FileSystemObject") Set objtxt = fsobj.OpenTextFile("./subfolder/foo.txt", 1) thearr = split(objtxt.readline, ",") document.write(thearr(0) & " and " & thearr(1)) </script>
I get the code to work when saving with asp extension but not when I save as html, is there a way to get it to work with only using the html extension? If not does someone have an explanation as to why scripting filesystemobject without the asp extension doesn't work? I seem to can't search for the answer I'm looking for.
-
Jo Jo over 10 yearsthanks, I wasnt really sure, im new to using iis and vbscript. What would you say is the best workaround to access that file and gather the data delimited by commas. Im considering rewriting it in javascript as we want to keep the .html extension.
-
Stephen Quan over 10 years@Jo Jo. I posted the best workaround in my answer below. Change the file suffix from .html to .hta. (i.e. see HTML Applications).