• 0

[Javascript] How to simulate a click on Firefox?


Question

Hello,

I need to click a button using Javascript. I searched the Internets and I found the following code:

	<script type="text/javascript">
		function buttonClick() {
			if (document.getElementById('send').dispatchEvent)
			{
				var mine = document.createEvent("MouseEvents");
				mine.initEvent("click", true, true);
				document.getElementById('send').dispatchEvent(mine);
			}
			else
			{
				document.getElementById('send').click();
			}
		}
	</script>

//body

<body onload="buttonClick();" onunload="buttonClick();">
</body>

At first, I was using: document.MyForm.Button.click(), it turned out that it only works with IE and throws an error in Firefox.

The above code is still working with IE, but not in Firefox and it doesn't throw an error or anything like that. It's like it's not there at all.

Any help is appreciated, thanks.

15 answers to this question

Recommended Posts

  • 0

Do not put the code you want in the button click event, put it into a function. Then call that function from the click - and you can call the function from whenever else you want too.

Or is there a situation that this would not allow?

  • 0

I'm not sure if I understood you correctly. The problem is that the button must be clicked programmatically. I have a form that I need to submit at runtime, and it requires that "button click". I'm trying to simulate a user clicking that button.

Could you give me an example please?

  • 0

<input type="button" onclick="alert('click')" />

Would become

function doClick(){
alert('click');
}

<input type="button" onclick="doClick()" />

Or even... If you want the sender to be available...

function doClick(sender){
alert(sender);
}

function simulateClick(objectId){
objMyButton = document.getElementById(objectId);
doClick(objMyButton);
}
<body onload="simulateClick('myButton')">
<input id="myButton" type="button" onclick="doClick(this)" />

  • 0

Thanks for your input, but that's not the problem I'm facing. The main issue is doing the click itself on the button, and not "onclick" or whatever. onclick implies that the button was clicked. I need to do the clicking before, right?

<body onload="simulateClick('myButton')">

That's not a click simulation, you're just calling alert(). I need a something like alert() that actually clicks the buttom (submitting the form).

  • 0

Yes and what I am saying is this:

Why do you need the physical button to be clicked?

Surely when clicked you want "something" to happen? Fine...

But shift this "something" out, and call it when the button is clicked.

You can then call it at another time also - simulating the same action as the button being clicked...

OR

Look into using jQuery? I believe this may help: http://stackoverflow.com/questions/343811/...ry-click-method

  • 0

I really appreciate your help, really. However :p, all I want is to click the button, nothing more. There is no logic, there are no routines ... nothing. It's just a matter of click that damned button !

<form id="MyForm" name="MyForm" method="post" action="https://linkgoeshere" enctype="multipart/form-data">
<input field><input field><input field>

<input type="submit" value="login" name="send" id="send" />
</form>

That's the form I have, and that's the button that must be clicked without the user actually clicking it.

Do you have *any* idea why Firefox does not allow this: document.MyForm.send.click() ? It's so simple, it works on IE but not on firefox ;(.

  • 0

I'm not sure why, but if I do form.submit() the user will be redirected to the actual form, with the fileds all filled up with the information, but he still have to click login.

The application I have at hand tried to hide all this overhead from the user. So he goes to our application, clicks a link (for example, to a specific provider), and continues without him logging in to every single provider.

For that reason, we submit that form (and all other forms) at runtime. There are some forms that works perfecty with the submit() function, and there are forms that oblige you to click the button. That's why I need to know how to click that button, or actually simulate the click as if the user actually clicked it.

Does that make any logical sense to anyone?

  • 0

Okay, lets take a form that submits a search term to Google.

<body onload="gForm.submit();">
	 <form name="gForm" method="get" action="http://www.google.com/search?hl=en&source=hp&aq=f&oq=&aqi="><input type="hidden" name="q" value="test" />
	 </form>
 </body>

That works perfectly fine, when you visit that page, you'll get redirected to Google with "test" as your search term.

I have almost the same forms, the information are coming from the database (username, password .. etc ..). As you can see, the form works fine with the submit() function. That's not the case when you have HTTPS.

I know that the form above works with the submit, but lets do it with a button, shall we?

<body>
	 <form name="gForm" method="get" action="http://www.google.com/search?hl=en&source=hp&aq=f&oq=&aqi="><input type="hidden" name="q" value="test" />
		 <input type="submit" value="submit" name="send" id="send" />
	 </form>
 </body>

How do I submit that form by clicking the button with Javascript?

If we did this:

<body[u][b] onload="gForm.send.click();"[/b][/u]>
	 <form name="gForm" method="get" action="http://www.google.com/search?hl=en&source=hp&aq=f&oq=&aqi="><input type="hidden" name="q" value="test" />
		 <input type="submit" value="submit" name="send" id="send" />
	 </form>
 </body>

It WILL work, but only in IE. Firefox throws an error.

I need to find a way to do it for both IE and Firefox.

  • 0

I'd recommend using JQuery, as it adds a nice transparency layer over the browser, so the same function call in JQuery will operate fine on both IE, Firefox, etc. Once you have referenced the JQuery library, do something like this:

$(function() {
  $("input.send").click);

Or, to just submit the form:

$(function() {
  $("form").submit();
});

The problem you will be seeing is that the click and submit actions are very different. A click event in javascript does not action a form, it literally is for clicking on the physical button. A submit event will actually do the form post.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Not Netflix's fault and they had to switch to content creation. Content owners jacked up prices, won't allow them renew, and didn't give them the option to license new content.
    • As sphbecker said you obviously missed the hidden /s at the end of my comment.
    • Everything I have ever read about Windows 11's telemetry collection over Windows 10 is that it's minor. Why would Microsoft be leaving data on the table with Windows 10? That doesn't even pass the smell test. LOL Windows 11 can be less performant in certain cases (and faster in others) but that has always been blamed on extra security features in WIn11. Windows 11 when I used it always ran fine for me. I would say If you were really concerned about either you wont be running Windows to begin with or limited to a secondary machine.
    • There was a brief time when they allowed Netflix (ad free) to pretty much license everything affordable, and piracy actually went down. When they saw how much Netflix was making, number of users, etc, they stopped allowing renewing of content and withholding it to make their own streaming services.
    • Wine 10.9 released bringing EGL support for all graphic drivers and several bug fixes by David Uzondu Wine 10.9 is out now, bringing EGL library support for all graphics drivers. For anyone trying to get Windows software running smoothly on Linux or macOS, this unification of EGL access promises more consistent graphical behavior and potentially better performance for applications that lean on it. EGL is a crucial bridge, letting things like OpenGL ES (often used in mobile apps, some desktop applications, and even games) talk to your system's window manager. Having it standardized across all drivers under Wine means fewer instances where an app might render beautifully on one setup but turn into a pixelated mess or refuse to launch on another. Gamers, and especially those dabbling with newer Windows titles, will appreciate that the bundled vkd3d, Wine's secret sauce for translating Direct3D 12 calls to Vulkan, has been upgraded to version 1.16. Every update to vkd3d generally means improved compatibility with the latest and greatest Direct3D 12 titles and ongoing fixes for stubborn ones. Considering vkd3d is a core component for projects like Steam's Proton, which enables a massive library of Windows games on Linux and the Steam Deck, this update is certainly welcome news. Key improvements vkd3d 1.16 brings include: DXIL shaders are now supported in the default configuration, raising the maximum supported shader model to 6.0. Graphics pipeline state objects can be created from shaders with embedded root signatures. Implementation of the SetEventOnMultipleFenceCompletion() method of the ID3D12Device1 interface. Experimental support for compiling DXIL shaders is now a fully supported and enabled-by-default feature in libvkd3d-shader. libvkd3d-shader has initial support for geometry shaders and various new intrinsic functions. The vkd3d demos now work on both Microsoft Windows and Apple macOS builds. Moving on, support for compiler-based exception handling with Clang also makes an appearance; this is a technical enhancement that can lead to greater stability for applications built using that specific compiler when they are run through Wine, reducing unexpected crashes for a subset of programs. Furthermore, Wine 10.9 introduces initial groundwork for generating Windows Runtime metadata in WIDL, its Interface Definition Language tool. As always with development releases, there is a solid list of bug fixes, 34 of them in this particular version. Some notable ones include resolving fullscreen issues with the classic 3D Pinball - Space Cadet, fixing display artifacts in Empire Earth, sorting out text display problems in the EA app launcher, and addressing crashes or visual glitches in games like Dyson Sphere Program and platforms like Geekbench 6. If you're interested in trying out this release, follow the installation instructions for your platform: Ubuntu/Debian, Fedora, or macOS. Installation guides for other platforms, including Slackware, are also available. The release notes for version 10.9 are available here.
  • Recent Achievements

    • First Post
      ClarkB earned a badge
      First Post
    • Week One Done
      Epaminombas earned a badge
      Week One Done
    • Week One Done
      Prestige Podiatry Care earned a badge
      Week One Done
    • Week One Done
      rollconults earned a badge
      Week One Done
    • Week One Done
      lilred1938 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      143
    2. 2
      Xenon
      130
    3. 3
      ATLien_0
      124
    4. 4
      +Edouard
      102
    5. 5
      snowy owl
      97
  • Tell a friend

    Love Neowin? Tell a friend!