Question

How to write sub hta in my main hta so that the sub hta is hidden in main hta and when clicking a button from main hta, the sub hta should pop up just like a window.

I don't want to program these main and sub hta's as 2 separate files and call sub hta from main hta.

Was it helpful?

Solution

There is a way -that I can think of- to do that but it's really painful. The same way Teemu mentioned. But If you insist on a single hta file, here's your code:

    <html>
    <head>
        <title>Main HTA</title>
        <HTA:APPLICATION
            SCROLL="no"
            INNERBORDER="no"
        />
    </head>
    <body>
    This is the main HTA <br /><br />
    <input type="button" value="open sub hta" onclick="runSubHTA(subhta)" />

    <script type="text/javascript">
        //  path to the new (sub) hta file
        var subhta = "subhta.hta";

        //  function to create and launch the sub hta file
        function runSubHTA(path) {

            //  creating the new hta file
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var sub = fso.CreateTextFile(subhta, true);

            //  writing to the created hta file
            sub.WriteLine("<title>Sub HTA<\/title>");
            sub.WriteLine("Welcome to the sub HTA <br />");

            //  this code will run on the sub hta before exit
            //  and it'll delete the sub hta file, meaning itself
            sub.WriteLine('<script type="text/javascript">');
            sub.WriteLine('var subhta = "subhta.hta";');
            sub.WriteLine('window.onbeforeunload = function() {');
            sub.WriteLine('var fso = new ActiveXObject("Scripting.FileSystemObject");');
            sub.WriteLine('var sub = fso.DeleteFile(subhta, 1);');
            sub.WriteLine('}<\/script>');

            //  closing the connection to the file, we're done writing
            sub.Close();

            //  launching the sub hta file
            WshShell = new ActiveXObject("WScript.Shell");
            WshShell.Run(path, 1, false);
        }
    </script>

    </body>
</html>

Coding 2 separate hta files would be much easier. This way you have to embed your sub hta code line by line. But there might be a better way to do it. Like writing the whole page in a single line with line-breaks and indenting. I haven't tried something like this before so I can't help on this. But if you find a way to do it, do share.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top