Jump to content

Call delphi method for JavaScript ?


eduardo.junqueira

Recommended Posts

I did not understand. It would be something like Step 1 ====================== Unit xxx txxx.procyyy procedure ; begin    run ggggg end ; step 2 ====================== page.html script javascript Call procedure txxx.procyyy ; the unit from html page
Link to comment
Share on other sites

If I understand you correctly, you can then use a unihtmlframe and trigger an

ajaxevent of that frame, from html, using

 

ajaxRequest(mainform.htmlframe, ['eventname'], { param1: some_info });

 

and then handle that event and trigger the delphi procedure.

Link to comment
Share on other sites

Hi,

 

Try, for example:

 

1. UniHTMLFrame1.HTML ...

<input type="button" onclick="ajaxRequest(MainForm.UniHTMLFrame1, 'call', [])" value="call method '_click'">

2.

  public
    { Public declarations }
    procedure _click;
  end;

3.

procedure TMainForm.UniHTMLFrame1AjaxEvent(Sender: TComponent;
  EventName: string; Params: TUniStrings);
begin
  if EventName = 'call' then
  begin
    _click;
  end;
end;

procedure TMainForm._click;
begin
  ShowMessage('call');
end;

Best regards.

 

Link to comment
Share on other sites

Are you talking about loading an URL in an UrlFrame, and then trying to

trigger any events in Unigui from that code?

 

It seems to me that you then do not have access to the ajaxRequest

function, so theoretically you could create a new XMLHttpRequest() object,

if you only knew the URL on the unigui receiving side.

<html>
<head>
</head>
<body>

<button type="button" onClick="xhr.send(encodeURI('eventname=test'));">Click Me!</button> 


<script type="text/javascript">
  xhr =  new XMLHttpRequest();
  xhr.open('GET',encodeURI('[Unigui-urlframe-url]'));
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  
  xhr.onload = function() {
    if (xhr.status === 200 && xhr.responseText !== newName) {
        alert('Something went wrong.  Name is now ' + xhr.responseText);
    }
    else if (xhr.status !== 200) {
        alert('Request failed.  Returned status of ' + xhr.status);
    }
};
 
  
</script>
</body>
</html> 
Link to comment
Share on other sites

  • 1 month later...

Hi,

 

Try, for example:

 

1. UniHTMLFrame1.HTML ...

<input type="button" onclick="ajaxRequest(MainForm.UniHTMLFrame1, 'call', [])" value="call method '_click'">

2.

  public
    { Public declarations }
    procedure _click;
  end;

3.

procedure TMainForm.UniHTMLFrame1AjaxEvent(Sender: TComponent;
  EventName: string; Params: TUniStrings);
begin
  if EventName = 'call' then
  begin
    _click;
  end;
end;

procedure TMainForm._click;
begin
  ShowMessage('call');
end;

Best regards.

 

Hi

I tried to use the code, but does not work.
Is there anything else to do?
 
att,
Link to comment
Share on other sites

  • 7 months later...

Hi , Delphi developer !! 

 

How are you ? 

 

I am testing of AjaxRequest()  to get  something  on  a Html file  designed by bootstrap . 

BTW ,  I can't get  any event  from the html .

I need your help !!

 

 

>  Delphi XE2 , Unigui 1.0.0.1386 , chrome latest 

 

After I put  an uniHTMLFrame named HTMLFrame on the MainForm

 

1.  HTMLFrame.HTML.LoadFromFile('index.html');      <----- designed html by bootstrap 

2.  added  Five buttons for testing AjaxRequest() 

3.  added  <script> function myfunc(){}</script>

 

           <!-- LOOK AT ME !!   buttons for testing calling server ------------------------------------------------>
 
           - Two buttons works ! -
 
               <button type="button" class="btn btn-default"  onclick="alert('OK'); ">alert</button>
               <button type="button" class="btn btn-default"  onclick="javascript: myfunc('OK'); "> myfunc </button>
 
           - below Others not work for me  - 
 
               <button type="button" class="btn btn-default"  onclick="ajaxRequest(MainForm.form , 'clickme', [] );" >call_form</button>
               <button type="button" class="btn btn-default"  onclick="ajaxRequest(MainForm.window , 'clickme', [] );" >call_window</button>
               <button type="button" class="btn btn-default"  onclick="ajaxRequest(MainForm.HTMLFrame , 'clickme', [] );">call_HTMLFrame</button>
 
Best regards,
David.
Link to comment
Share on other sites

Hi David,

 

In your code, you are loading an html file:

 

procedure TMainForm.UniFormShow(Sender: TObject);
begin
     HTMLFrame.HTML.LoadFromFile('index.html');
end;

 

And in this html file you have a redirect:

 

<script language="javascript">
    window.location.href = "pages/index.html"
</script>

 

So in effect you are kicked out of Unigui and the scope of the ajaxRequest function.

 

If you change your onShow event to:

 

 

procedure TMainForm.UniFormShow(Sender: TObject);
begin
     HTMLFrame.HTML.LoadFromFile('pages/index.html');
end;

- you will stay in Unigui and catch the ajaxRequest.

 

Rather load the correct html file into the HTMLFrame directly

and adjust the paths to the other css and js files.

 

Kaj
 

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...