Click to See Complete Forum and Search --> : simulating link click with .click()


mercurialorg
10-16-2003, 12:23 PM
Hey all,

A tough problem to pick your brains about! I apologize in advance about the long post; there is a lot of back-and-forth involved and since I'm not sure what might cause a problem I thought the whole process should be shown.

We have content available for download on our site, which requires that the user accept a downloader agreement the first time they attempt to download anything. After the first time, they can freely download whatever they like, until they log out and log back in.

The first part I'm handling with the following function:

function clickCheck(myLink) {

// show downloader notice by default

var show_notice = 1;

// if the user has previously accepted the downloader agreement,
// they will have a cookie named "downloader" -- if so, do not
// display the downloader agreement

var cookie_list = document.cookie;
if(cookie_list.indexOf("DOWNLOADER") != -1) {
show_notice = 0; }

// if the downloader agreement is to be displayed, capture the DOM
// location of the link to auto-click the link after the user accepts
// the agreement and stop the link for this click

if(show_notice) {
for (var i=0; i<=(document.links.length-1) && document.links[i].href != myLink; i++) { }
user_window = window.open('download_agree.cfm?link_loc=' + i,'_blank','');
return false; } } // end clickCheck();


which is called from the link like so:

<a href="file.cfm" onClick="return clickCheck(this);show_content('info');">content</a>

so that if the user does not have the "downloader" cookie, the clickCheck() function will return false and prevent the content from showing. When the child window displaying the downloader agreement is launched, the DOM location of the link is passed to it in the URL and held in a hidden form field.

If the agreement is accepted, the child window attempts to run a second function (below) in the parent window, which simulates the user clicking the link again so the user will only have to accept the agreement to view the content, rather than accepting the agreement (one click) and then clicking again on the content they want to download (evil second click).

The child window calls the parent window function as follows:

top.opener.userClick(<cfoutput>#form.link_loc#</cfoutput> );

with the DOM location of the link dynamically evaluated via ColdFusion. I have tried calling the function both with and without single quotes around the 'link_loc' value and it doesn't seem to make a difference.

The userClick() function in the parent window is:


function userClick(link_loc) {
document.links[link_loc].click(); } // end userClick();


which generates the following error:

"Error: document.links[link_loc].click is not a function"

If I hardcode the DOM link location as document.links[37].click(); the link clicks properly and all is happy. If I place alert('document.links[' + link_loc + '].click();'); in the userClick() function, the alert text correctly displays the click command. If I try eval("document.links[" + link_loc + "].click()"); it causes the error:

"Error: document.links[37].click is not a function"

I've also tried clicking the link in the parent window directly from the child window with:

top.opener.document.links[<cfoutput>#form.link_loc#</cfoutput>].click();

and other variations, none of which seem to work.

Basically, everything works fine except for the link being clicked after the downloader agreement is accepted. I have tried as many ways as I can think of to get the link to click via the .click() method, which I just found yesterday so I'm not familiar with the ins and outs. It seems to work only if I can hardcode the link's DOM location, which won't work because there are a number of links on the page which the user could click. Any help would be greatly appreciated, as I have been wracking my brain about this and so far have come up empty.

Thanks in advance :)

mercurialorg
10-16-2003, 01:24 PM
I did some testing by writing a bare-bones page with just the functionality I am trying to do here and it works fine. So it looks like my problem is not in this specific thing that I am trying to do but caused by a problem somewhere else in the page that is interfering with my code. :rolleyes: