Javascript onClick

Discussion in 'JavaScript' started by egdcltd, Aug 5, 2005.

  1. #1
    Is it possible to use the onClick command to call up another file? or can it interface directly with a MySQL database?
     
    egdcltd, Aug 5, 2005 IP
  2. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not sure what you are asking. JS runs client-side. All you can do is to send a request, which may do the DB work you need. For example:

    function onClick(event)
    {
    window.location = "domain/db-handler.ext?params";
    }

    params will contain whatever you want to pass to MySQL.

    J.D.
     
    J.D., Aug 5, 2005 IP
  3. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, looking at my request, it wasn't very clear.

    What it is, in my directory, there are coded and straight html links. The coded links, when clicked, as well as sending the user to the site, also run a cgi script (out.cgi?id=xxxx) that adds 'points' to the clicked sites' score for sorting the order of sites in the directory, by communicating with the MySQL database.

    I got the impression that it would be possible to use onClick to do the same function.
     
    egdcltd, Aug 5, 2005 IP
  4. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can do this with JS - create a hidden frame and change its src on click (add link ID as a search argument). Another approach is to use redirect - hit the counting script first and pass the actual URL as a search argument. Use 302 redirect to point the browser to the actual page.

    J.D.
     
    J.D., Aug 5, 2005 IP
  5. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I don't want to use a redirect - I'm trying to remove the redirects and replace them with straight html links.

    Would the other method function like a straight html link for seo purposes?
     
    egdcltd, Aug 6, 2005 IP
  6. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #6
    JS will do what you need (when available, that is). You other and better option is to analyze your logs. I would rather do this.

    J.D.
     
    J.D., Aug 6, 2005 IP
  7. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I presume you mean manually check the outgoing clicks for each link. Unfortunately, that isn't really possible, with over 2000 links and climbing. The scoring is part of the link ordering of the directory, and really needs to be automated. Either I keep the current version, change to JavaScript, or abandon the scoring system completely.
     
    egdcltd, Aug 6, 2005 IP
  8. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Sorry, I shoul've been more specific. Here's some script to get you started:

    <script type="text/javascript">
    function onClick(href)
    {
    document.getElementById("tracker").src = "tracker.app?url=" + encodeURI(href);
    return true;
    }
    </script>

    <img id="" src="some-image">
    <a href="outgoing-link" onclick="return onClick(href)">click me</p>

    As a result of this, a request will be made to the tracker.app page with the URL passed as a search argument. All you will need to do is to analyze your logs for these search arguments.

    J.D.
     
    J.D., Aug 6, 2005 IP
  9. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I'm afraid I'm not quite sure what you mean here. Could you explain it a bit more please?
     
    egdcltd, Aug 6, 2005 IP
  10. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #10
    When people click on the link, the function is called. This function changes image's src attribute so that it includes the actual link. At this point, the browser will make a request to your server for this image, including url= part. Then it will go to the actual link. As a result, your logs will contain the hits for this image and search arguments will contain url=outgoing-link, which is what you are looking for. Try this (it won't actually send the link, but will show you the image):

    <script type="text/javascript">
    function onClick(href)
    {
    document.getElementById("tracker").src = "http://forums.digitalpoint.com/images/misc/dps_logo.gif?url=" + encodeURIComponent(href);
    return false;
    }
    </script>

    <img id="tracker" src="http://forums.digitalpoint.com/images/misc/dps_logo.gif">
    <a href="http://forums.digitalpoint.com" onclick="return onClick(href)">click me</p>

    Before clicking on the link, check image's properties. The click on the link and check the properties again. This is what will end up in your log file. Once you change the return value to true, the browser actually will go to the href location.

    Also, there's a typo in the previous example - the id should say tracker (or whatever you choose instead). It is also better to use encodeURIComponent instead (see above).

    J.D.
     
    J.D., Aug 6, 2005 IP
  11. JohnM2009

    JohnM2009 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    JD ... This was helpful but I'm having trouble getting it to work using your exact code. Is it possible my hosting or server settings are not allowing the LOGGING. Basically, the original IMG is logged and the OnClick goes to the link... but the updated IMG with the URL is not getting LOGGED. Does this go to the ACCESS Log? Any help is appreciated! ... John M
     
    JohnM2009, Oct 31, 2009 IP