Forum Moderators: open

Message Too Old, No Replies

Firefox: click() event not working

         

kevsh

9:05 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



I have a bit of code that redirects a visitor to a url specified in the A tag's ID:
<A ID="myLink" HREF="target-page.html">

In the JavaScript I am using this code to redirect the visitor, which works fine in IE:

<SCRIPT LANGUAGE="JavaScript">
function startClick() {
myLink.click();
}
</SCRIPT>

The event is triggered in the BODY tag:
<BODY OnLoad="startClick()" ... >

The page redirects upon loading in IE, but not Firefox - does anyone know how to fix it to work in both? Or a workaround/alternative solution?

Many thanks in advance!

Bernard Marx

11:19 pm on Mar 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think the click method is IE only.
More importantly, your referencing method is IE only. You can only use id's as object reference variables in IE (+ some others). Use document.getElementById instead (and stop learning Javascript at MS sites! :) )

1. Get the href from the link, and relocate:

function startClick() {
window.location.href = document.getElementById('myLink').href
}

2. ..but you might as well just hard-code the URL

window.location.href = 'target-page.html';

3. While you're at it. If this is a redirect, it will be less irritating if you remove the redirecting page from history, or the user will be stuck without a sensible back button:

window.location.replace('target_page.html');

kevsh

12:37 am on Mar 8, 2005 (gmt 0)

10+ Year Member



Thanks for the tip - it does work, but what I was hoping to do is simulate a user click.

It's important for tracking at the destination site, but if log files will track the code you provided as a "click" then it should be okay. (Sorry, I don't know much about how web servers track referrers/clicks, etc. in log files)

Bernard Marx

2:11 am on Mar 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The log should record the request for 'target_page', and referrer, just like any other - click or no.

SpaceFrog

9:35 am on Mar 8, 2005 (gmt 0)

10+ Year Member



have you tried trying to trigger click with :

object.onclick()

Bernard Marx

11:09 am on Mar 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think there'd need to be an onclick handler on the anchor.
All in all, it's a very long route round just to redirect a page, which can be done with a simple statement.