Executing JavaScript in a web browser using Delphi

Similar Question:
How to trigger the OnChange event of a "Select" element in Delphi WebBrowser?

Hey there,

I have a TWebBrowser in Delphi that loads a webpage containing a form with a dropdown menu. I am able to select an item from the dropdown, but the select element has an onchange event. How can I execute the onchange event in Delphi without using execScript('yourfunctioname()', 'JavaScript');

Here is the select element with the onchange event:

<select align="left" id="carrierNameDropDown_UNSHIPPEDITEMS" onChange="MYO.ES.OtherCarrierToggle (this, 'UNSHIPPEDITEMS' )">
          <option value="0"  selected="1" >Select</option>
              <option value="Chronopost"  >Chronopost</option>
              <option value="City Link"  >City Link</option>
              <option value="DHL"  >DHL</option>
              <option value="DPD"  >DPD</option>
              <option value="Deutsche Post"  >Deutsche Post</option>
              <option value="Fastway"  >Fastway</option>
              <option value="FedEx"  >FedEx</option>
              <option value="GLS"  >GLS</option>
              <option value="GO!"  >GO!</option>
              <option value="Hermes Logistik Gruppe"  >Hermes Logistik Gruppe</option>
              <option value="La Poste"  >La Poste</option>
              <option value="Parcelforce"  >Parcelforce</option>
              <option value="Parcelnet"  >Parcelnet</option>
              <option value="Poste Italiane"  >Poste Italiane</option>
              <option value="Royal Mail"  >Royal Mail</option>
              <option value="SDA"  >SDA</option>
              <option value="Smartmail"  >Smartmail</option>
              <option value="TNT"  >TNT</option>
              <option value="Target"  >Target</option>
              <option value="UPS"  >UPS</option>
              <option value="Yodel"  >Yodel</option>

          <option value="Other">
              Specify carrier:
          </option>
        </select>

I managed to achieve this by:

if EmbeddedWB1.Document <> nil then begin
if EmbeddedWB1.Document.QueryInterface(IHTMLDocument3,docb) = S_OK then begin
elb := docb.getElementById('carrierNameDropDown_UNSHIPPEDITEMS');
if elb <> nil then begin
(elb as IHTMLSelectElement).value := 'Parcelforce';
OleVariant (elb as IHTMLElement). FireEvent ('onchange');
end;

Answer №1

Here is a sample code snippet that demonstrates how to achieve the desired functionality:

uses 
  MSHTML;

procedure TBrowserPageIE.Test;
var
  doc : IHTMLDocument3;
  el  : IHTMLElement;
  v   : OleVariant;
begin
  if FBrowser.Document <> nil then begin
    if FBrowser.Document.QueryInterface(IHTMLDocument3,doc) = S_OK then begin
      el := doc.getElementById('carrierNameDropDown_UNSHIPPEDITEMS');

      if el <> nil then begin
        (el as IHTMLSelectElement).value := 'UPS';
        (el as IHTMLElement3).FireEvent('onchange', v);
      end;
    end;
  end;
end;

Another approach to achieve the same result is using late binding:

procedure TBrowserPageIE.Test;
var
  doc : IHTMLDocument3;
  el  : OleVariant;
  v   : OleVariant;
begin
  if FBrowser.Document <> nil then begin
    if FBrowser.Document.QueryInterface(IHTMLDocument3,doc) = S_OK then begin
      el := doc.getElementById('carrierNameDropDown_UNSHIPPEDITEMS');
      el.value := 'UPS';
      el.FireEvent('onchange', v);
    end;
  end;
end;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Pair a specific portion of text with another string

I'm having trouble finding a substring within a string. The substring and the string are as follows: var str="My name is foo.I have bag(s)" var substr="I have bag(s)" When I use str.match(substr), it returns null, probably because the match() funct ...

Angular 2: Applying class to td element when clicked

I am working with a table structured like this <table> <tbody> <tr *ngFor="let row of createRange(seats.theatreDimension.rowNum)"> <td [ngClass]="{'reserved': isReserved(row, seat)}" id={{row}}_{{sea ...

Redirecting users from one page to another using Javascript along with

I am currently working on a project that employs the use of a PHP <? header('Location: <a href="http://url.com" rel="nofollow">http://url.com</a>'); ?> for handling redirects. I find this method particularly effective because of ...

Keep track of the current state as the page changes with React Router

I am experiencing an issue with my React component. const Page = React.createClass({ getInitialState() { return { page: {} }; }, componentDidMount() { const pageId = this.props.params.pageId; socket.emit('get page', pageId, (pa ...

What is preventing window.scrollTo() from being executed?

I have implemented Bootstrap's Buttons plugin to toggle the state of a custom checkbox. When the button is toggled on, a hidden element should appear at the top of the page and then the page should scroll to the top. Similarly, when the button is togg ...

What steps can you take to convert your current menu into a responsive design?

I am currently developing a website using PHP, HTML, and CSS. The site is not responsive, and I want to make the menu responsive. My global navigation and admin interface are not adapting properly as they are designed using tables. Is there a method I can ...

Reorganize the placement of table columns between different rows

I am currently using a software program that automatically generates a form based on the selected options. The code for this form is generated in tables, which I am unable to directly edit. However, I would like to have the Amount, radio buttons, and their ...

Modal does not close

I am experiencing an issue with closing a modal. I have checked jQuery and everything seems to be working fine. Currently, I am using version 1.12.4 and have the following script tag in the head of my code: <script src="https://ajax.googleapis.com/aja ...

Retrieving a variable within a try-catch statement

I am trying to implement a function: function collect_user_balance() { userBalance = 0; try { var args = { param: 'name'; }; mymodule_get_service({ data: JSON.stringify(args), s ...

Different ways to conditionally set the expanded prop for the Material UI TreeView component

I am currently working on implementing a tree select feature similar to the one found in antd's tree-select using material ui components. Specifically, I have a TextField and TreeView components from Material-UI stacked vertically. Initially, I want t ...

Issue with loading controllers in route files [Node.js]

I've encountered an issue while trying to include a user controller into my routes for a node js app. Everything was working fine until suddenly it started throwing an error message. I've thoroughly checked the code for any potential issues but s ...

Toggling the form's value to true upon displaying the popup

I have developed an HTML page that handles the creation of new users on my website. Once a user is successfully created, I want to display a pop-up message confirming their creation. Although everything works fine, I had to add the attribute "onsubmit= re ...

Using .after() in AngularJS for nested ng-repeat recursive iteration

I have a straightforward layout function adjustLinks($scope) { $scope.links = [ { text: 'Menu Item 1', url: '#', },{ text: 'Menu Item 2', url: '#' ...

Enrolling JavaScript documents and connected inline programming

I need a solution for dealing with two arrays. The first array consists of URLs pointing to JavaScript files that I want to register using $.getScript(url); Next, the second array contains inline JavaScript commands that should be registered with $("html ...

What is the technique for invoking methods of the Joi class without the need to instantiate an object?

I recently delved into using the Joi NPM module and found it confusing that although it is described as a class in the documentation, it does not require creating a class object like var joi = new Joi();. Can you explain how this works? My understanding o ...

Deleting JSON files using Discord and Node.js by requiring them from a specific folder

Currently, I am in the process of developing a Discord bot using Node.js. One of the functions within the bot utilizes JSON files to store information about specific entities. I aim to create a command in Discord that, when called with a specific name asso ...

Show a table with rows that display an array from a JSON object using JavaScript

My current code includes JSON data that I need to display in table rows, but I'm struggling to understand how to do so effectively. The output I am currently seeing is all the rows from the array stacked in one row instead of five separate rows as in ...

Unable to display nested JSON data from API in Vue.js

Having trouble accessing nested properties from API JSON data. The Vue component I'm working on: var profileComponent = { data : function() { return { isError : false, loading : true, users : null, ...

Refreshing a React page will lead to props being empty

The situation I am facing is that the code appears to be correct, but for some reason, when the web page loads, this.props.items does not seem to be passed to Item. Strangely, when I attempt to display this.props.items in console.log, nothing shows up. How ...

Display PickerIOS when a button is clicked in a React Native application

I am a beginner with the react framework and I'm trying to implement a PickerIOS component on button click. However, I'm having trouble understanding how to call other classes upon an event. I found this code snippet on the React Native site: v ...