473,804 Members | 3,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with simulating a link.click

Hello All,

I have some problem by simulating a link click using javascript.
The webpage uses a js-library named interface (jQuery like)
------------------------------
<a id="foo" href="#">Try try try</a>
$('#foo').click (function(){
$('ul',this.par entNode).BlindT oggleVertically (500);
this.blur();
return false;
});
------------------------------

I tried first:
------------------------------
var elem = document.getEle mentById('foo') ;
elem.focus();
elem.click(); // does not work, and js becomes blocked
------------------------------

And then I tried:
------------------------------
var elem = document.getEle mentById('foo') ;
elem.focus();
if (elem.onclick || elem.onclick() !== false) {
alert('passed') ;
} else {
alert('failed') ; // Program goes here :-(
}
------------------------------

Is it possible to simulate a link click?

I have an idea:
1. Retrieve the coordinate of the link.
2a. Simulate a mouse click at this position, or
2b. Simulate a keystroke "ENTER".

But I have no idea how to implement this. Can someone help me?

Thanks in advance ^^)

--
Xu, Qian (stanleyxu)
http://stanleyxu2005.blogspot.com
Aug 23 '08 #1
5 3810
On Aug 24, 8:42*am, "Xu, Qian" <quian...@stud. tu-ilmenau.dewrote :
Hello All,

I have some problem by simulating a link click using javascript.
The webpage uses a js-library named interface (jQuery like)
------------------------------
<a id="foo" href="#">Try try try</a>
$('#foo').click (function(){
I don't know the library, but if it's "jQuery-like", then its $
function will return a native object, not the element itself. When
you call its click method, it will attach a listener to the element's
*onclick* property.

[...]
I tried first:
------------------------------
var elem = document.getEle mentById('foo') ;
elem.focus();
elem.click(); // does not work, and js becomes blocked
That will only work with inline listeners, it won't work with
dynamically added listeners. Since the listener is on the onclick
property, you need to use:

elem.onclick();

------------------------------

And then I tried:
------------------------------
var elem = document.getEle mentById('foo') ;
elem.focus();
if (elem.onclick || elem.onclick() !== false) {
* *alert('passed' );} else {

* *alert('failed' ); // Program goes here :-(}
Because the handler was added dynamically.

Is it possible to simulate a link click?
Yes, using dispatchEvent for W3C compatible browsers, or fireEvent for
those that use the IE model. But it is not well supported.

I have an idea:
1. Retrieve the coordinate of the link.
2a. Simulate a mouse click at this position, or
2b. Simulate a keystroke "ENTER".
The event must be dispatched to the correct DOM element, you can't do
it using the above method.

But I have no idea how to implement this. Can someone help me?
Search this group for "How to trigger event programmaticall y?"
<URL:
http://groups.google.com.au/group/co...cea9cdf065a524
>

--
Rob
Aug 24 '08 #2
SAM
Xu, Qian a écrit :
Hello All,

I have some problem by simulating a link click using javascript.
The webpage uses a js-library named interface (jQuery like)
------------------------------
<a id="foo" href="#">Try try try</a>
$('#foo').click (function(){
$('ul',this.par entNode).BlindT oggleVertically (500);
this.blur();
return false;
});
------------------------------

I tried first:
------------------------------
var elem = document.getEle mentById('foo') ;
elem.focus();
elem.click(); // does not work, and js becomes blocked
The JS function click() works only with buttons of forms
(ie : submit)

Is it possible to simulate a link click?
var elem = document.getEle mentById('foo') ;
if (elem.href) location = elem.href;
or (tested in Fx, Safari, Opera) :

var elem = document.getEle mentById('foo') ;
if(elem.onclick ) elem.onclick();
--
sm
Aug 24 '08 #3
RobG wrote:
On Aug 24, 8:42 am, "Xu, Qian" <quian...@stud. tu-ilmenau.dewrote :
>Hello All,

I have some problem by simulating a link click using javascript.
The webpage uses a js-library named interface (jQuery like)
------------------------------
<a id="foo" href="#">Try try try</a>
$('#foo').clic k(function(){

I don't know the library, but if it's "jQuery-like", then its $
function will return a native object, not the element itself. When
you call its click method, it will attach a listener to the element's
*onclick* property.

[...]
>I tried first:
------------------------------
var elem = document.getEle mentById('foo') ;
elem.focus() ;
elem.click() ; // does not work, and js becomes blocked

That will only work with inline listeners, it won't work with
dynamically added listeners. Since the listener is on the onclick
property, you need to use:

elem.onclick();

>------------------------------

And then I tried:
------------------------------
var elem = document.getEle mentById('foo') ;
elem.focus() ;
if (elem.onclick || elem.onclick() !== false) {
alert('passed') ;} else {

alert('failed') ; // Program goes here :-(}

Because the handler was added dynamically.

>Is it possible to simulate a link click?

Yes, using dispatchEvent for W3C compatible browsers, or fireEvent for
those that use the IE model. But it is not well supported.

>I have an idea:
1. Retrieve the coordinate of the link.
2a. Simulate a mouse click at this position, or
2b. Simulate a keystroke "ENTER".

The event must be dispatched to the correct DOM element, you can't do
it using the above method.

>But I have no idea how to implement this. Can someone help me?

Search this group for "How to trigger event programmaticall y?"
<URL:
http://groups.google.com.au/group/co...cea9cdf065a524
--
Rob
Thanks you both.

fireEvent() and dispatchEvent() do help.
But they do not work with normal link, do they?

Currently I am using the following code:
------------------------------------------------
elem.target = '';
elem.focus();
// attempt 1
try
{
if (elem.click()) {
return true;
}
}
catch(e) { /* This element does not support click */ }
// attempt 2
if (document.creat eEvent)
{
var evtObj = document.create Event('MouseEve nts');
if (evtObj && elem.dispatchEv ent && evtObj.initMous eEvent)
{
evtObj.initMous eEvent(
'click',
true, true, // Click events bubble and they can be cancelled
document.defaul tView, // Use the default view
1, // Just a single click
0, 0, 0, 0, // Don't bother with co-ordinates
false, false, false, false, // Don't apply any key modifiers
0, // 0 - left, 1 - middle, 2 - right
null); // Click events don't have any targets other than
// the recipient of the click
return elem.dispatchEv ent(evtObj);
}
}
else if (document.creat eEventObject)
{
return elem.fireEvent( 'onclick');
}
// attempt 3
if (elem.href)
{
location = elem.href;
return true;
}
return false;
-----------------------------------------------------------

--
Xu, Qian (stanleyxu)
http://stanleyxu2005.blogspot.com
Aug 24 '08 #4
On Aug 24, 11:30*am, "Xu, Qian" <quian...@stud. tu-ilmenau.dewrote :
[...]
>
Thanks you both.

fireEvent() and dispatchEvent() do help.
But they do not work with normal link, do they?
By "work" I guess you mean follow the link, the short answer is no,
though Firefox v2 and earlier did and Safari 3 still does. IE never
did.

Currently I am using the following code:
------------------------------------------------
elem.target = '';
elem.focus();
// attempt 1
try
{
* *if (elem.click()) {
I don't see the point of that, will calling the click function always
return true (or at least non-falsey)?
* * *return true;
* *}}

catch(e) { /* This element does not support click */ }
// attempt 2
if (document.creat eEvent)
As you know, at least some browsers that support createEvent will not
follow the link, so this seems pointless.

[...]
else if (document.creat eEventObject)
And IE doesn't follow the link either...

[...]
if (elem.href)
{
* *location = elem.href;
* *return true;}

return false;
If that's what you want to do, why not just do it and forget the
rest? It will work much more reliably in more browsers than anything
else that's been posted.
--
Rob
Aug 24 '08 #5
RobG wrote:
On Aug 24, 11:30 am, "Xu, Qian" <quian...@stud. tu-ilmenau.dewrote :
[...]

By "work" I guess you mean follow the link, the short answer is no,
though Firefox v2 and earlier did and Safari 3 still does. IE never
did.
Yes, I mean to simulate a manual link click. It is more than to follow a
link.

I don't see the point of that, will calling the click function always
return true (or at least non-falsey)?
Yes, you are right. I did not know. But a try-catch-block is useful.
Once an element does not support click() method, program will not be abort.

As you know, at least some browsers that support createEvent will not
follow the link, so this seems pointless.

[...]
>else if (document.creat eEventObject)

And IE doesn't follow the link either...
No, I try to fire onClick event.

[...]

If that's what you want to do, why not just do it and forget the
rest? It will work much more reliably in more browsers than anything
else that's been posted.
I want to simulate a user link click without the knowledge, how the html
looks like. It should work generally for all kind of links:
<a href="somewhere .htm">Normal Link</a>
<a onClick="alert( 123)">OnClick Only</a>
<a href="#" onClick="a_func tion()">OnClick with an indirect link</a>
<a href="#" id="aaa">The onClick event of this link will be hooked
dynamically</a>
.... and more variants, that I do not know.

--
Xu, Qian (stanleyxu)
http://stanleyxu2005.blogspot.com
Aug 24 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3554
by: Thanks | last post by:
I have a routine that is called on Page_Init. It retrieves folder records from a database which I display as Link Buttons in a table cell. I set the table cell's bgcolor to a default color (say black for example). I am dynamically creating the LinkButton controls and adding them into the table cell and I've also hooked up an event handler for each LinkButton's Click event. This all works fine. Now in the Link Button's Click event...
4
13857
by: Roger Withnell | last post by:
I would like to freeze column and row headings on a webpage, simulating freeze panes as in an Excel spreadsheet. Don't seem to be able to do it with Frames. Is there a way with Javascript and/or CSS and or Frames? Thanks in anticipation.
7
2485
by: daveyand | last post by:
Is there a way in javascript to trigger the clicking of the left mouse button. Basically i have a floating div that is translucent (40% opacity) this div covers the whole screen. when a user clicks on this div it disappears. What i would like is if the mouse happens to be over a link then that link should trigger. I imagine i can do a long way which'll involve where is the mouse now, what anchor is there, if its a link then set the...
3
3133
by: equazcion | last post by:
Hi, I have an image reference (IMG) in my page that changes depending on the value of a database field. Clicking the image triggers an Ajax call to change the database field (toggles the field value between 1 and 0). I've made it work so that the image source changes to reflect the new value once the Ajax call is complete. In other words, the image reflects the value of the database field in real-time. Kind of like a real-time checkbox. This...
1
1167
by: paratge | last post by:
Hi, I want to do a click on a picture in an iframe, and i can't manage to make my function works fine. Here is the code : <html> <head> <script language="JavaScript" type="text/javascript"> var sHN=1;
25
2443
by: Jonno | last post by:
Hi I am having a problem with session vars being propagated between pages on this site: http://www.meettheancestors.com/sessiontest/index.php If you enter any user id and password and click Log In (no actual validation is performed), and then move around the other pages and/or keep refreshing the pages it will eventually display something that is incorrect i.e. saying your logged in when you aren't or vice versa. The exact same code...
4
4317
by: oliver james | last post by:
I'm trying to automate an Excel spreadsheet from Access. I've established a link and loaded data from a recordset onto a worksheet in Excel. I now want to perform some manipulation on the data. I previously set up a command button in the spreadsheet to run the VBA to do this manipulation; this VBA is stored in the button's Click event. Now that I am learning about Automation, I would like to "call" this VBA from Access. Can anyone tell...
7
2554
by: maaravi | last post by:
Hi, I'm trying to simulate clicking on a link, but Firefox seems to disable it. In the sample below, when clicking on the div tag, the onclick event of the link is triggered, but the browser doesn't move to the new location. could it have to do with security measures? <html xmlns="http://www.w3.org/1999/xhtml" > <head>
2
3186
by: empiresolutions | last post by:
i need to simulated having clicked on a link with JavaScript. As you will see in my stripped down example the resulting function from the click is a window.addEvent() action. Thanks for the help. js code - window.addEvent('domready', function() { expandCollapse(); } function expandCollapse(){ $('expand').addEvent('click', function(){ // do something
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.