Solved

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

Posted on 2004-10-22
2,644 Views
Last Modified: 2008-01-09
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
0
Question by:arterberry
    12 Comments
     
    LVL 9

    Expert Comment

    by:ginsonic
    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');
    0
     
    LVL 9

    Expert Comment

    by:ginsonic
    Add:

    uses
      MSHTML_TLB, SHDocVw, ShellAPI;
    0
     

    Author Comment

    by:arterberry
    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.
    0
     
    LVL 9

    Expert Comment

    by: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.
    0
     

    Author Comment

    by:arterberry
    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.

     
    0
     

    Author Comment

    by:arterberry
    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.
    0
     
    LVL 9

    Accepted Solution

    by:
    Hmmm! What IE use you ? And what OS ?

    My fullWEB browser work perfect for specified link.
    0
     

    Author Comment

    by:arterberry
    Hello ginsonic,
    I am using Windows XP (Service Pack 2) and IE 6.
    0
     

    Author Comment

    by:arterberry
    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
    0
     
    LVL 9

    Expert Comment

    by: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.
    0
     
    LVL 9

    Expert Comment

    by:ginsonic
    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 !?!?
    0
     
    LVL 9

    Expert Comment

    by:ginsonic
    no comments?
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: JavaScript Coding - Massive 12-Part Bundle

    Regardless of your programming skill level, you'll go from basics to advanced concepts in a vast array of JavaScript subjects including Sammy.js, Agility.js, Ember.js, Node.js, jQuery, AJAX, Extjs, AngularJS, Knockout.js, and JSON.

    A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now