Link to home
Start Free TrialLog in
Avatar of arterberry
arterberry

asked on

How do you enable JavaScript with TWebBrowser(actually SHDocVW)?

I have a simple browser application designed in Delphi, and I use the ActiveX control SHDocVw. I navigated around and I encountered several errors when using a site that has Flash! I started using the EmbeddedWB (from www.euromind.com) and I still recieve errors - mainly a crash when the Flash animation stops and a JavaScript call shakes the screen (visit Disney.com - there is a Flash presentation of "The Incredibles" and then the JavaScript function that is called afterwards does a screen shake).

I guess this is a browser-side feature that can be trapped or caught. I want to know what to do on this one - other than disabling the Flash and script capability... I know I can do that as a last resort. I just want to trap the issue and dispay the animation as it should be displayed. I know there must be a way!

Please supply any code snippets...

I checked on things like this...

WebBrowser1.OleObject.Document.ParentWindow.ExecScript(...parameters...)

But this would require a great deal of parsing for certain JavaScript calls (at least I think it would).


Thanks for your help -

-John
Avatar of ginsonic
ginsonic
Flag of Romania image

Not sure if I understand, but you can call a Java script with:

procedure ExecuteScript(doc:IHTMLDocument2;script:string;language:string);
var
 win: IHTMLWindow2;
 Olelanguage: Olevariant;
begin
 if doc <> nil then
 begin
   try
     win := doc.parentWindow;
     if win <> nil then
     begin
       try
         Olelanguage := language;
         win.execScript(script, Olelanguage);
       finally
         win := nil;
       end;
     end;
   finally
     doc := nil;
   end;
 end;
end;

ExecuteScript(WebBrowser1.Document as
HTMLDocument2,'MyJava()','JavaScript');
Add:

uses
  MSHTML_TLB, SHDocVw, ShellAPI;
Avatar of arterberry
arterberry

ASKER

Thank you for your help, ginsonic -

I like this - but I have one quick review question...when I use this function -

ExecuteScript(WebBrowser1.Document as HTMLDocument2,'MyJava( )','JavaScript');

What if I do not know the name of the script - for example - 'MyJava( )'?
Do I have to hard-code the name of script strings in the ExecuteScript call each time?

In other words, I want to create an open-ended support for JavaScript with my browser application. In the event that my browser application encounters JavaScript - I would like for it to just run it.

Is that possible?

Again, thank you ginsonic.
I tested with my application fulWEB browser and look that all is perfect for me on disney.com. Can you download my browser from www.construiesc.ro/fullWEB and test it ?

I can't get any error for specified address and I use EmbeddedWB component, too.
Thanks ginsonic -

I just checked (with your browser (VERY NICE!!!))!

I saw that Disney removed the "shake" example ... so I found this instead...

http://www.javascriptkit.com/javatutors/advwin3.shtml

And I scrolled down and clicked on the "Shake Window" button (with your browser). And your browser worked flawlessly.

My browser does fail when I go and shake the window.

I will add this to my code -

ExecuteScript(WebBrowser1.Document as HTMLDocument2,'MyJava( )','JavaScript');

along with the function you wrote. I will just use the generic 'MyJava( )' string and run this link

http://www.javascriptkit.com/javatutors/advwin3.shtml

- to see what will happen.

 
Hello ginsonic,

I have – well – attempted to use this procedure. I have provided the code here. I designed this in Delphi 5, and I am using the browser wrapper from Euromind. I have included my code here – in full, below.  When I go to the link (http://www.javascriptkit.com/javatutors/advwin3.shtml), and scroll down – and then select the “Shake Window” button – my web browser goes blank. I look forward to your response and again – thank you for your help. I will provide points once this thing is working!


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  OleCtrls, SHDocVw_TLB, StdCtrls, MSHTML_TLB, ShellAPI;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    WebBrowser1: TWebBrowser;
    Scriptlet1: TScriptlet;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure ExecuteScript(doc:IHTMLDocument2;script:string;language:string);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
     WebBrowser1.Navigate(Edit1.Text);
end;

procedure TForm1.ExecuteScript(doc:IHTMLDocument2;script:string;language:string);
var
 win: IHTMLWindow2;
 Olelanguage: Olevariant;
begin
 if doc <> nil then
 begin
   try
     win := doc.parentWindow;
     if win <> nil then
     begin
       try
         Olelanguage := language;
         win.execScript(script, Olelanguage);
       finally
         win := nil;
       end;
     end;
   finally
     doc := nil;
   end;
 end;
end;


end.
ASKER CERTIFIED SOLUTION
Avatar of ginsonic
ginsonic
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello ginsonic,
I am using Windows XP (Service Pack 2) and IE 6.
Is the code okay?
I know that I have no clue as to how to implement the procedure into my code. Could you run through and compile the code and apply some magic to this crazy problem.

Thanks ginsonic
I don't use any special code into my browser to use the java. And how you can see work perfect for me. I will make some test on weekend (sorry no time untill then) to see what happening and what make my browser to work and your no.

At this moment my opinion is that you use some code that affect the normal function of the EmbeddedWB.

P.S. I suggest to update your IE to the last version and use EventSinkImp from
http://www.euromind.com/iedelphi/gettingstarted/usefulTools.htm
to install again your component.
The single solution that I can figure out is to set your WindowState to wsMaximized and the border form to bsNone. In this case work perfect. Else :(

Of course the browser must be set to alClient.

Tested and work perfect. Don't know is is a wrapper or OS problem !?!?
no comments?