简体   繁体   中英

How my HTML application treats relative addresses depends too much on how it is run

My HTML application file F:\\relroot\\libname.lib\\textapp.hta (name altered) contains the following function:

function fullNameOfFile(filename) { 
     var fso = newMedium(); return fso.GetAbsolutePathName(filename)
}

When I run the application via Open or double-click it in the directory,

fullNameOfFile("../etc.txt") returns "F:\\relroot\\etc.txt" [runs perfectly!].

When I run it via Open with:Microsoft HTML Apln Host , however:

fullNameOfFile("../etc.txt") returns "C:/Windows/etc.txt" [wrong number!].

How should I code to get to work regardless of how the application is run?

I think I might have "Aspie-rigged" a solution (or maybe two)

Apparently opening the HTML application via Open (or double-click) presets the "current" directory to the directory containing the app while opening via Open With : Microsoft® HTML Application Host uses a default directory (like « C:\\WINDOWS\\system32 »)

var shell = new ActiveXObject("WScript.Shell");
var wasDir = shell.currentDirectory  /* OPTIONAL: push original current directory
                                            to allow restoration later */
    var swDL = false;                      // for differences in device file format
    var splitter = (swDL ? '/'   : '\\')   // foreslash v. backslash
    var slashes  = (swDL ? '///' : '//')   /* slashes between scheme/protocol
                                                and authentication/host */
var lhref = swDL ? unescape(location.href) : document.URL;
var newDir = lhref.slice(location.protocol.length + slashes.length,
                   lhref.lastIndexOf(splitter));
/*
//  uncomment this block for an "inside window"

alert("Entering Directory: «" + wasDir + "»\n" +
         "Protocol/Scheme: «" + location.protocol + "»\n" +
           "Location HREF: «" + lhref + "»\n" +
         "Trial Directory: «" + newDir + "»");
*/
shell.currentDirectory = newDir

It seems to work like a charm on my system (Dell PC, Internet Explorer/¿Edge). I threw in some comments as well in case the next user needs to tweak it for their system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM