Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#136420 - 22/01/2003 13:10 JavaScript/WSH question
tonyc
carpal tunnel

Registered: 27/06/1999
Posts: 7058
Loc: Pittsburgh, PA
I have a piece of JavaScript (run from Windows Scripting Host) which creates an IE window with some HTML in it. There are also a few JavaScript methods defined in the HEAD of the HTML document. What I want to do is call the methods in the HEAD block from the JavaScript which created the HTML document. I can't figure out how to do it. Any ideas?

Here is a simple example of what I'm trying to accomplish:


var oIE = WScript.CreateObject("InternetExplorer.Application");
oIE.navigate("about:blank");
var doc1 = oIE.Document;
oIE.visible = 1;

doc1.open();
doc1.writeln("<HTML>");
doc1.writeln("<HEAD>");
doc1.writeln("<script id=\"myscr\" language=\"JavaScript1.2\">");
doc1.writeln("function foobar() {");
doc1.writeln(" alert(\"foobar\")");
doc1.writeln("}");
doc1.writeln("</SCRIPT>");
doc1.writeln("</HEAD>");
doc1.writeln("<BODY>");

doc1.writeln("This is some body text.");
doc1.writeln("</BODY>");
doc1.writeln("</HTML>");
doc1.close();

// do something here to call the foobar() method within the IE window

.

This script, when run from Windows Scripting Host, creates the IE window which contains a JavaScript function foobar() inside it. I want to call that foobar() method from the above script, *not* by clicking a button in IE or anything like that.

Ideas?
_________________________
- Tony C
my empeg stuff

Top
#136421 - 22/01/2003 13:26 Re: JavaScript/WSH question [Re: tonyc]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
Why not just go ahead and put a call into the HTML? So that the finished HTML would look like:
    <HTML>
    
    <HEAD>
    <script id="myscr" language="JavaScript1.2">
    function foobar() {
    alert("foobar")
    }
    foobar();
    </SCRIPT>
    </HEAD>
    <BODY>
    This is some body text.
    </BODY>
    </HTML>
Otherwise, how about:
    oIE.Navigate("javascript:foobar()");
Note that I haven't tested this much at all, so it may simply not work.
_________________________
Bitt Faulk

Top
#136422 - 22/01/2003 13:33 Re: JavaScript/WSH question [Re: wfaulk]
tonyc
carpal tunnel

Registered: 27/06/1999
Posts: 7058
Loc: Pittsburgh, PA
Why not just go ahead and put a call into the HTML?

'cause I don't wanna do that.

Because the whole thing I'm trying to piece together is a script that programatically updates the contents of the IE window based on MIDI input from a program called MIDI-OX which has some pretty powerful scripting capabilities using the Windows Scripting Host. So I want updates to the HTML in the browser to be dynamic based on input from MIDI (my electronic drum kit.) The ultimate goal is to have a graphic representation of what I'm playing.

So far I've already got the mechanics of updating the HTML dynamically based on the MIDI input. The next step was to embed some JavaScript in the document to allow for some more tricker things with images and stuff.

So, just placing the method in the HTML would call it once at the beginning, which isn't what I want. I want the ability to call those methods from the WSH JavaScript whenever I want.

Your second example, using oIE.navigate, seems to be what I'm looking for. I just didn't think of telling the browser to "navigate" to a method that's already contained in the current document. But really, I guess that's the way to do it. Not very intuitive, but it looks like it works.
_________________________
- Tony C
my empeg stuff

Top
#136423 - 22/01/2003 13:41 Re: JavaScript/WSH question [Re: tonyc]
number6
old hand

Registered: 30/04/2001
Posts: 745
Loc: In The Village or sometimes: A...
There is a "scripts" object/collection available under the Document object (which in your code var1 has a handle to), so this may be the place to look.[for the picture of Object model Click Here (Microsoft MSDN site).

If you have Office XP, the htmlref.chm help file is supposed to document all this - I don't have Office XP, so don't have that file on my system.

Once you have right script object from the scripts collection, I'd be surprised if you cannot call the functions in that script externally using a method sun run or execute etc - but the script object would have to expose such a method.






Top
#136424 - 22/01/2003 14:01 Re: JavaScript/WSH question [Re: number6]
tonyc
carpal tunnel

Registered: 27/06/1999
Posts: 7058
Loc: Pittsburgh, PA
That is EXACTLY what I thought, but the MSDN docs don't give any indication that traversing through the Document.Scripts collection lets you get access to any methods.

For now, I think the navigate() solution will work for me. Who knows, that might even be the "right way" to do this.
_________________________
- Tony C
my empeg stuff

Top