Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Loading a function when a hyperlink is clicked

Posted on 2003-11-14
8
Medium Priority
?
492 Views
Last Modified: 2013-12-24
Hello Everyone,

I am having problems loading a function (logs the users visit to a page) when a user clicks on a hyperlink.  Here is the code I am using:

<a href="Some URL" target="_blank" onclick="<CFOUTPUT>#traffic("This is the title of the page","This is the URL of the page")#</CFOUTPUT>">My Link</a>

I have four more identical links and a few regular links.  The problem I am having is when any link is clicked (even the regular ones) the above code executes for every link, even though it was not the one being clicked.

Any help with this would be appreciated.
0
Comment
Question by:gardine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Expert Comment

by:tomkinite
ID: 9749606
Make sure your recordset (if you're using one) is moving to new records.  For example, if "This is the title of the page" is a value from a database, it should change while in a <CFOUTPUT QUERY=""> or a <CFLOOP QUERY="">.  Check the resulted source to verify that those 2 variables are actually changing.

Your method of logging isn't very practical, though.  For those with disabled, or unavailable Javascript, this method would not work.

You are also asking the browser to execute a CF function in an OnClick event.  I would hope to assume that the resulted #traffic()# function renders another Javascript function that the browser can use.  

I think what you are trying to do is execute a CF function when a user clicks a link, but CF renders completely before Javascript, therefore impossible.  Use the onClick event to run a typical Javascript UDF such as onClick="<cfoutput>userVisit('#titleofpage#','#urlofpage#')</cfoutput>".  This will be fairly hokey and I don't recommend it.  If all of your pages are CF pages, simply use Application.cfm to track page hits.  Application.cfm is a page you put in your root directory of your application.  CF will run Application.cfm before rendering the actual pages being requested.
0
 
LVL 17

Expert Comment

by:anandkp
ID: 9752898
Ur code can better go as :

<CFQuery name="x">
select * from table
</CFQuery>

<CFOUTPUT Query="x"><!--- gets u the links in the DB - as u mentioned u have 4 in ur case --->
       <a href="#Url#" target="_blank" onclick="#traffic('#Title#','#Url#')#">#LinkName#</a>
</CFOUTPUT>

& that shld be it !
0
 
LVL 4

Expert Comment

by:procept
ID: 9753289
Hi,

you are using #'s around the function name, do you want to call a CF function? You can't do that without sending the page back to the server. The CF function is executed server side, but, the click is executed by the user, client side. You can only call a function of a client side language, like JS for example.

HTH,

Chris
0
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

 
LVL 1

Author Comment

by:gardine
ID: 9780895
Thank you everyone for your advice.  I dont think I was specific enough.  I created the above function to create a log when a user visits a specific page (I do not want to track all of them).  So, for every page I would like to track I place the fonction in it.  This leads me to my problem.  When I link to a pdf file and open it in a new window I am unable to load my function.
0
 
LVL 1

Expert Comment

by:tomkinite
ID: 9780989
Wusage (www.boutell.com)

That's the high road.
0
 
LVL 1

Author Comment

by:gardine
ID: 9781007
High Road?
0
 
LVL 1

Accepted Solution

by:
tomkinite earned 150 total points
ID: 9781069
Yes.  Log Analyzer.  You really can't do this the way you explain.  CF is rendered BEFORE Javascript.  The OnClick event is a Javascript event.  Essentially you are telling Javascript to run a function that it has no perception of.  

To track ALL clicks you have 2 options.
1. Log Analyzer
2. Download Counter Script

Each link is a "download.cfm?file=file.pdf"

Download.cfm gets the file's size, etc, logs the download to the database and then shoots it to the user using CFHEADER and CFCONTENT.

I've done this "download.cfm" method before and could give you the code if you wish to do it this way.
0
 
LVL 1

Author Comment

by:gardine
ID: 9826288
Thank You.  I took the idea and created my own solution.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question