Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatic click link/button

Status
Not open for further replies.

icrf

Programmer
Dec 4, 2001
1,300
US
I'm trying to automate clicking a link, but it seems IE/Firefox only runs the onclick event handlers, but doesn't actually follow the href. Opera and Safari do both, which is what I'm after.

Here's a test page with code:
Code:
<[COLOR=#0000FF]!DOCTYPE[/color] [COLOR=#0000FF]html[/color] [COLOR=#FF0000]PUBLIC[/color] [COLOR=#808080]"-//W3C//DTD XHTML 1.0 Strict//EN"[/color] [COLOR=#808080]"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"[/URL][/color]>
<[COLOR=#0000FF]html[/color] [COLOR=#FF0000]xmlns[/color]=[COLOR=#808080]"[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL][/color]>
<[COLOR=#0000FF]head[/color]>
<[COLOR=#0000FF]title[/color]>test</[COLOR=#0000FF]title[/color]>
<[COLOR=#0000FF]script[/color] [COLOR=#FF0000]type[/color]=[COLOR=#808080]"text/javascript"[/color]>
[COLOR=#006600]<!--[/color]
[COLOR=#0000FF]function[/color] clickElement(elementid){
	[COLOR=#FF0000]var[/color] e = document.getElementById(elementid);
	[COLOR=#0000FF]if[/color] ([COLOR=#FF0000]typeof[/color] e == [COLOR=#808080]'object'[/color]) {
		[COLOR=#006600]/*if(typeof e.click != 'undefined') {
			e.click();
			alert('click');
			return false;
		}
		else */[/color]
		[COLOR=#0000FF]if[/color](document.createEvent) {
			[COLOR=#FF0000]var[/color] evObj = document.createEvent([COLOR=#808080]'MouseEvents'[/color]);
			evObj.initEvent([COLOR=#808080]'click'[/color],[COLOR=#0000FF]true[/color],[COLOR=#0000FF]true[/color]);
			e.dispatchEvent(evObj);
			alert([COLOR=#808080]'createEvent'[/color]);
			[COLOR=#0000FF]return[/color] [COLOR=#0000FF]false[/color];
		}
		[COLOR=#0000FF]else[/color] [COLOR=#0000FF]if[/color](document.createEventObject) {
			e.fireEvent([COLOR=#808080]'onclick'[/color]);
			alert([COLOR=#808080]'createEventObject'[/color]);
			[COLOR=#0000FF]return[/color] [COLOR=#0000FF]false[/color];
		}
		[COLOR=#0000FF]else[/color] {
			e.click();
			alert([COLOR=#808080]'click'[/color]);
			[COLOR=#0000FF]return[/color] [COLOR=#0000FF]false[/color];
		}
	}
}
[COLOR=#006600]//-->[/color]
</[COLOR=#0000FF]script[/color]>
</[COLOR=#0000FF]head[/color]>
<[COLOR=#0000FF]body[/color]>
<[COLOR=#0000FF]p[/color]><[COLOR=#0000FF]a[/color] [COLOR=#FF0000]id[/color]=[COLOR=#808080]"foo"[/color] [COLOR=#FF0000]href[/color]=[COLOR=#808080]"javascript:alert('href')"[/color] [COLOR=#FF0000]onclick[/color]=[COLOR=#808080]"alert('onclick')"[/color]>programatically click me</[COLOR=#0000FF]a[/color]></[COLOR=#0000FF]p[/color]>
<[COLOR=#0000FF]p[/color]><[COLOR=#0000FF]a[/color] [COLOR=#FF0000]href[/color]=[COLOR=#808080]"#"[/color] [COLOR=#FF0000]onclick[/color]=[COLOR=#808080]"clickElement('foo')"[/color]>click me for real</[COLOR=#0000FF]a[/color]></[COLOR=#0000FF]p[/color]>
</[COLOR=#0000FF]body[/color]>
</[COLOR=#0000FF]html[/color]>
IE (versions 6 & 7) and Firefox (version 2) don't pop up "href" like Opera and Safari do. Uncommenting that first chunk uses the old click() method, which still exists in IE, but not Firefox. Does anyone have any suggestions how this might be possible?

- Andrew
Text::Highlight - A language-neutral syntax highlighting module in Perl
also on SourceForge including demo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top