Re: Programatically trigger an onClick event?
Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<40F81709. 2D92F1E5@agrico reunited.com>.. .[color=blue]
> Henry Lafleur wrote:
>[color=green]
> > "Berislav Lopac" <berislav.lopac @dimedia.hr> wrote in message news:<cd7ubv$ls r$1@ls219.htnet .hr>...[color=darkred]
> > > Mike Gratee wrote:
> > > >> var link = document.getEle mentById['yourLinksIdAtt rbuteValue'];
> > > >> link.click();
> > > >
> > > > Great - thanks alot. However I don't think that will work with
> > > > Netscape 6, Mozilla or Opera right? How would I change it to work
> > > > with those?
> > >
> > > Have you tried? If not, why do you think if it won't work? If yes, what
> > > exactly happens?
> > >
> > > Berislav[/color]
> >
> > It does not work with FireFox 9.1 (latest) which runs Mozilla Gecko engine:
> > ...
> > Henry.[/color]
>
> This might work for you:
>
> <a href="http://www.yahoo.com" onclick="alert( 'hi');return true;">Yahoo!</a>
> <form>
> <input type="button" onclick="naviga teLink(0);" value="Click link index 0">
> </form>
> <script type="text/javascript">
> function navigateLink(li nkIndex) {
> var link = document.links[linkIndex];
> if (link.onclick && link.onclick()) {
> if (link.target) {
> window.open(lin k.href, link.target);
> } else {
> window.location .href = link.href;
> }
> }
> }
> </script>
>
> Of course it won't completely mimic the user clicking the link, if the link has a target and Javascript
> is disabled, a user-initiated click would still open a new window (assuming that TARGET attributes are
> honored by the user agent). Also, a TARGET attribute probably has a better chance of opening a new
> window then window.open() does, given the current state of popup blockers.[/color]
Grant,
The only issues here are that this:
[color=blue]
> if (link.onclick && link.onclick()) {[/color]
would return false if there was no onclick event. The link would only
be followed if it had an onclick event in this case. It may be better
to use a variable here that is either true or the result of the
link.onclick().
! var bolOnClick = true;
! if (link.onclick) bolOnClick = link.onclick();
! if (bolOnClick) {[color=blue]
> if (link.target) {[/color]
....
or maybe (keeping with the terse code):
! if (!link.onclick || link.onclick && link.onclick())[color=blue]
> if (link.target) {[/color]
....
which makes me want to say, "Yikes!"
Another small issue is:
[color=blue]
> window.location .href = link.href;[/color]
would not honor the <base target=... setting. This may need to read:
[color=blue]
> if (link.target) {
> window.open(lin k.href, link.target);
> } else {[/color]
! var strTarget = null;
! var elesBase;
! // Use the DOM to get the base (if available)
! if (document.getEl ementsByTagName ) {
! elesBase = document.getEle mentsByTagName( 'base');
! if (elesBase.lengt h > 0) {
! strTarget = elesBase[0].getAttribute(' target');
! }
! }
! if (strTarget) {
! window.open(lin k.href, strTarget);
! } else {[color=blue]
> window.location .href = link.href;[/color]
! }[color=blue]
> }[/color]
or (eliminating redundancy some):
! if (!link.onclick || link.onclick && link.onclick())
! var strTarget = null;
! var elesBase;[color=blue]
> if (link.target) {[/color]
! strTarget = link.target;
! } else {
! // Use the DOM to get the base (if available)
! if (document.getEl ementsByTagName ) {
! elesBase = document.getEle mentsByTagName( 'base');
! if (elesBase.lengt h > 0) {
! strTarget = elesBase[0].getAttribute(' target');
! }
! }
! }
! if (strTarget) {
! window.open(lin k.href, strTarget);
! } else {[color=blue]
> window.location .href = link.href;[/color]
! }
! }
This makes the code much uglier, but more robust.
[color=blue]
> Also, a TARGET attribute probably has a better chance of opening a new
> window then window.open() does, given the current state of popup blockers.[/color]
The popup blockers are a big pain for legit web developers. We use
popups to clear concurrent licenses when the user closes the browser,
so that may not work anymore (more a problem for our customers than
for us). Especially since now IE will include popup blocking. (I can
see the bugs in that streaming in!)
Fortunately, most of the apps I write are for an intranet or a trusted
internet site. This means we can set minimum browser requirements
above what we can do for internet sites.
Thanks for the feedback,
Henry.
Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<40F81709. 2D92F1E5@agrico reunited.com>.. .[color=blue]
> Henry Lafleur wrote:
>[color=green]
> > "Berislav Lopac" <berislav.lopac @dimedia.hr> wrote in message news:<cd7ubv$ls r$1@ls219.htnet .hr>...[color=darkred]
> > > Mike Gratee wrote:
> > > >> var link = document.getEle mentById['yourLinksIdAtt rbuteValue'];
> > > >> link.click();
> > > >
> > > > Great - thanks alot. However I don't think that will work with
> > > > Netscape 6, Mozilla or Opera right? How would I change it to work
> > > > with those?
> > >
> > > Have you tried? If not, why do you think if it won't work? If yes, what
> > > exactly happens?
> > >
> > > Berislav[/color]
> >
> > It does not work with FireFox 9.1 (latest) which runs Mozilla Gecko engine:
> > ...
> > Henry.[/color]
>
> This might work for you:
>
> <a href="http://www.yahoo.com" onclick="alert( 'hi');return true;">Yahoo!</a>
> <form>
> <input type="button" onclick="naviga teLink(0);" value="Click link index 0">
> </form>
> <script type="text/javascript">
> function navigateLink(li nkIndex) {
> var link = document.links[linkIndex];
> if (link.onclick && link.onclick()) {
> if (link.target) {
> window.open(lin k.href, link.target);
> } else {
> window.location .href = link.href;
> }
> }
> }
> </script>
>
> Of course it won't completely mimic the user clicking the link, if the link has a target and Javascript
> is disabled, a user-initiated click would still open a new window (assuming that TARGET attributes are
> honored by the user agent). Also, a TARGET attribute probably has a better chance of opening a new
> window then window.open() does, given the current state of popup blockers.[/color]
Grant,
The only issues here are that this:
[color=blue]
> if (link.onclick && link.onclick()) {[/color]
would return false if there was no onclick event. The link would only
be followed if it had an onclick event in this case. It may be better
to use a variable here that is either true or the result of the
link.onclick().
! var bolOnClick = true;
! if (link.onclick) bolOnClick = link.onclick();
! if (bolOnClick) {[color=blue]
> if (link.target) {[/color]
....
or maybe (keeping with the terse code):
! if (!link.onclick || link.onclick && link.onclick())[color=blue]
> if (link.target) {[/color]
....
which makes me want to say, "Yikes!"
Another small issue is:
[color=blue]
> window.location .href = link.href;[/color]
would not honor the <base target=... setting. This may need to read:
[color=blue]
> if (link.target) {
> window.open(lin k.href, link.target);
> } else {[/color]
! var strTarget = null;
! var elesBase;
! // Use the DOM to get the base (if available)
! if (document.getEl ementsByTagName ) {
! elesBase = document.getEle mentsByTagName( 'base');
! if (elesBase.lengt h > 0) {
! strTarget = elesBase[0].getAttribute(' target');
! }
! }
! if (strTarget) {
! window.open(lin k.href, strTarget);
! } else {[color=blue]
> window.location .href = link.href;[/color]
! }[color=blue]
> }[/color]
or (eliminating redundancy some):
! if (!link.onclick || link.onclick && link.onclick())
! var strTarget = null;
! var elesBase;[color=blue]
> if (link.target) {[/color]
! strTarget = link.target;
! } else {
! // Use the DOM to get the base (if available)
! if (document.getEl ementsByTagName ) {
! elesBase = document.getEle mentsByTagName( 'base');
! if (elesBase.lengt h > 0) {
! strTarget = elesBase[0].getAttribute(' target');
! }
! }
! }
! if (strTarget) {
! window.open(lin k.href, strTarget);
! } else {[color=blue]
> window.location .href = link.href;[/color]
! }
! }
This makes the code much uglier, but more robust.
[color=blue]
> Also, a TARGET attribute probably has a better chance of opening a new
> window then window.open() does, given the current state of popup blockers.[/color]
The popup blockers are a big pain for legit web developers. We use
popups to clear concurrent licenses when the user closes the browser,
so that may not work anymore (more a problem for our customers than
for us). Especially since now IE will include popup blocking. (I can
see the bugs in that streaming in!)
Fortunately, most of the apps I write are for an intranet or a trusted
internet site. This means we can set minimum browser requirements
above what we can do for internet sites.
Thanks for the feedback,
Henry.
Comment