473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.parentNode).BlindToggleVertically(500) ;
this.blur();
return false;
});
------------------------------

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

And then I tried:
------------------------------
var elem = document.getElementById('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 3774
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.getElementById('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.getElementById('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 programmatically?"
<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.parentNode).BlindToggleVertically(500) ;
this.blur();
return false;
});
------------------------------

I tried first:
------------------------------
var elem = document.getElementById('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.getElementById('foo');
if (elem.href) location = elem.href;
or (tested in Fx, Safari, Opera) :

var elem = document.getElementById('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').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.getElementById('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.getElementById('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 programmatically?"
<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.createEvent)
{
var evtObj = document.createEvent('MouseEvents');
if (evtObj && elem.dispatchEvent && evtObj.initMouseEvent)
{
evtObj.initMouseEvent(
'click',
true, true, // Click events bubble and they can be cancelled
document.defaultView, // 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.dispatchEvent(evtObj);
}
}
else if (document.createEventObject)
{
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.createEvent)
As you know, at least some browsers that support createEvent will not
follow the link, so this seems pointless.

[...]
else if (document.createEventObject)
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.createEventObject)

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_function()">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
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...
4
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...
7
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...
3
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...
1
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">...
25
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...
4
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...
7
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...
2
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. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.