Clicked Link Countdown

Reply

Join Date: Aug 2005
Posts: 102
Reputation: martinkorner is an unknown quantity at this point 
Solved Threads: 0
martinkorner's Avatar
martinkorner martinkorner is offline Offline
Junior Poster

Clicked Link Countdown

 
0
  #1
Feb 15th, 2006
Is it possible to have a script on my site which says something like:
"Please click on at least 3 of the links below to continue to the download."

Then, when the user clicks one of the specified links it opens in a new window and this page says:
"Please click on at least 2 more of the links below."

Then, when the user clicks another of the specified links it opens in a new window and this page says:
"Please click on at least 1 more of the links below."

Then, when the user clicks on a third of the specified links it opens in a new window and this page says:
"Thank you, you may now click here to download the file"

Would it also be possible for the script to only count clicks on certain links?

Hope you can help!
Martin
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Clicked Link Countdown

 
0
  #2
Feb 15th, 2006
Of course. Set the onclick attribute of these links to run a script. That script can increment a counter, alter the text within a control, open windows, etc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 102
Reputation: martinkorner is an unknown quantity at this point 
Solved Threads: 0
martinkorner's Avatar
martinkorner martinkorner is offline Offline
Junior Poster

Re: Clicked Link Countdown

 
0
  #3
Feb 15th, 2006
Where could I get a script that would do that (I'm not advanced enough to write my own yet)?

Thanks
Martin
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Clicked Link Countdown

 
0
  #4
Feb 15th, 2006
I have no idea. Let me politely urge you to BECOME advanced enough. This isn't the "where can I download scripts" forum, it's the "how do I write scripts" forum. You'll get plenty of help, from me and others. Let's start by defining the tasks your script needs to accomplish:
  1. maintain a counter
  2. alter the contents of an element
  3. open a new window
There. The task seems more managable, now, doesn't it?

Let's write some HTML markup as a test harness. We just need some simple hyperlinks. When the user clicks a link, all we want to do is task #1, increment a counter.

First, the HTML:

HTML and CSS Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5.  
  6. <a href="#" id="link_01">Hyperlink 1</a><br />
  7. <a href="#" id="link_02">Hyperlink 2</a><br />
  8. <a href="#" id="link_03">Hyperlink 3</a>
  9.  
  10. </body>
  11. </html>

Simple enough. We don't want these links to go anywhere... the script will eventually do that for us. So, we set the HREF property to a hash-mark, which is essentially the top of the page.

Now, let's implement a counter, by adding to our HTML:

HTML and CSS Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4.  
  5. var counter = 0;
  6. function link_click()
  7. {
  8. counter++;
  9. alert(counter);
  10.  
  11. }
  12.  
  13. </script>
  14. </head>
  15. <body>
  16.  
  17. <a href="#" id="link_01" onclick="link_click();">Hyperlink 1</a><br />
  18. <a href="#" id="link_02" onclick="link_click();">Hyperlink 2</a><br />
  19. <a href="#" id="link_03" onclick="link_click();">Hyperlink 3</a>
  20.  
  21.  
  22. </body>
  23. </html>

When the user clicks a link, it runs the script. The script increments the counter and displays the current value of the counter via an alert.

Digest that, ask any questions you have, and let me know when you're ready to move onto step #2.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 102
Reputation: martinkorner is an unknown quantity at this point 
Solved Threads: 0
martinkorner's Avatar
martinkorner martinkorner is offline Offline
Junior Poster

Re: Clicked Link Countdown

 
0
  #5
Feb 16th, 2006
Right - that's all digested!

To make the counter start at 3 would I change:
HTML and CSS Syntax (Toggle Plain Text)
  1. var counter = 0;
to
HTML and CSS Syntax (Toggle Plain Text)
  1. var counter = 3;


And to make the counter count down from 3 change:
HTML and CSS Syntax (Toggle Plain Text)
  1. counter++;
to
HTML and CSS Syntax (Toggle Plain Text)
  1. counter--;

And to change the message:
HTML and CSS Syntax (Toggle Plain Text)
  1. alert(counter);
to
HTML and CSS Syntax (Toggle Plain Text)
  1. alert("Please click on at least " + (counter) + " more links to continue to your download.");

I've tried all these changes and they do work! :lol:

Then to make the script write it on the page I would put in:
HTML and CSS Syntax (Toggle Plain Text)
  1. document.write("Please click on at least " + (counter) + " more links to continue to your download.");
under the "alert" command - but this overwrites the links.

So I could put:
HTML and CSS Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. <!--
  3.   document.write("Please click on at least " + (counter) + " more links to continue to your download.");
  4. // -->
  5. </script>
in the body of the page - but this stays at 3 when I click on a link.

How do I do this bit??? ???


Thanks for the help so far...
Martin
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Clicked Link Countdown

 
0
  #6
Feb 16th, 2006
You're on the right track... "document.write()" is deprecated, meaning, it's old and newer browsers may eventually stop supporting it. I doubt it, frankly, but it's best to use a newer method. So, onward to step 2.

One such method is to use the .innerHTML property of an element. Note, this works in all major browsers, but isn't technically part of an HTML specification. I'm showing it to you because it makes the concept very clear. In the final script, it should be changed to one of the DOM methods. Just file that away for "stuff to learn later".

Consider this version of our test page:

HTML and CSS Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4.  
  5. var counter = 3;
  6. function link_click()
  7. {
  8. counter--;
  9. if (counter != 0)
  10. {
  11. document.getElementById("myMessage").innerHTML = "Please click on at least " + counter + " of the links below.";
  12. }
  13. else
  14. {
  15. document.getElementById("myMessage").innerHTML = "Proceed to download.";
  16. }
  17. }
  18.  
  19. </script>
  20. </head>
  21. <body>
  22.  
  23. <p id="myMessage">Please click on at least 3 of the links below.</p>
  24.  
  25. <a href="#" id="link_01" onclick="link_click();">Hyperlink 1</a><br />
  26. <a href="#" id="link_02" onclick="link_click();">Hyperlink 2</a><br />
  27. <a href="#" id="link_03" onclick="link_click();">Hyperlink 3</a><br />
  28.  
  29. </body>
  30. </html>

We've added a paragraph. Our script decrements the counter, and dynamically changes the text of the paragraph, using the value of our counter inside the text.

If the counter reaches zero, we change the paragraph completely.

You should research "document.getElementById()" and ".innerHTML", as they are the keys to this functionality.

Digest that, and we'll move on to step 3.
Last edited by tgreer; Feb 17th, 2006 at 5:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 102
Reputation: martinkorner is an unknown quantity at this point 
Solved Threads: 0
martinkorner's Avatar
martinkorner martinkorner is offline Offline
Junior Poster

Re: Clicked Link Countdown

 
0
  #7
Feb 17th, 2006
Right...next:lol:

Martin
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Clicked Link Countdown

 
1
  #8
Feb 17th, 2006
Next is to open a window. In this version, we add back our HREF's so the links actually go somewhere.

Notice the new onclick attributes. It now looks like:

return link_click(this);

What that does is tell the link that we expect the function to "return" a true or false. If a "false" is returned, then we "cancel" the click. Why? Because we will do the navigation in our script, we don't want the actual hyperlink to do what it would normally do: navigate to the page.

We also pass in a parameter called "this". "This" is a special keyword that passes in the current object, which is our hyperlink. Notice the function? It will store "this" in a variable, "x".

We added a "window.open()" statement. You need to research that. We pass in a URL. What URL? x.href, the URL value from the hyperlink's HREF attribute.

That will open the page in a new window.

Lastly, we return false, to cancel the hyperlink click. If we didn't do that, our main page would ALSO navigate, which we don't want to do.

HTML and CSS Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4.  
  5. var counter = 3;
  6. function link_click(x)
  7. {
  8. counter--;
  9. if (counter != 0)
  10. {
  11. document.getElementById("myMessage").innerHTML = "Please click on at least " + counter + " of the links below.";
  12. }
  13. else
  14. {
  15. document.getElementById("myMessage").innerHTML = "Proceed to download.";
  16. }
  17.  
  18. window.open(x.href,'');
  19. return false;
  20. }
  21.  
  22. </script>
  23. </head>
  24. <body>
  25.  
  26. <p id="myMessage">Please click on at least 3 of the links below.</p>
  27.  
  28. <a href="http://www.tgreer.com" id="link_01" onclick="return link_click(this);">T.Greer's Site</a><br />
  29. <a href="http://www.daniweb.com" id="link_02" onclick="return link_click(this);">Daniweb</a><br />
  30. <a href="http://www.google.com" id="link_03" onclick="return link_click(this);">Search Engine</a><br />
  31.  
  32. </body>
  33. </html>
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 102
Reputation: martinkorner is an unknown quantity at this point 
Solved Threads: 0
martinkorner's Avatar
martinkorner martinkorner is offline Offline
Junior Poster

Re: Clicked Link Countdown

 
0
  #9
Feb 17th, 2006
WOW - Thanks alot for all the help so far!!! :lol:

So I would put:
HTML and CSS Syntax (Toggle Plain Text)
  1. document.getElementById("myMessage").innerHTML = "Thank you, You may now click " + "<a href='download.html'>here</a>" + " to proceed to the " + "<a href='download.html'>download</a>" + ".";
to make a link to the downloads page.

A few more questions:
1) How do I make the script write the "proceed to download" text even when there are -1 clicks needed or -2 etc.

2) Would it be possible to open the new windows in the background - so my page will stay on top until the user clicks on the tab at the bottom of the screen to view the page.

3) Will this script work in all browsers, or will I have to provide another link directly to the download for certain browsers?

Thanks
Martin
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Clicked Link Countdown

 
0
  #10
Feb 17th, 2006
Originally Posted by martinkorner
WOW - Thanks alot for all the help so far!!! :lol:

So I would put:
HTML and CSS Syntax (Toggle Plain Text)
  1. document.getElementById("myMessage").innerHTML = "Thank you, You may now click " + "<a href='download.html'>here</a>" + " to proceed to the " + "<a href='download.html'>download</a>" + ".";
to make a link to the downloads page.
That should work. Test it. You can place any string as the value of ".innerHTML", so play with your tags etc. until you get what you want.

1) How do I make the script write the "proceed to download" text even when there are -1 clicks needed or -2 etc.
You would alter this line if (counter != 0) . The part inside the parantheses is called an "expression". Right now the expression tests to see if counter is not equal to 0. You would change the expression to test to see if counter is less than or equal to 0: if (counter <= 0) .

2) Would it be possible to open the new windows in the background - so my page will stay on top until the user clicks on the tab at the bottom of the screen to view the page.
Maybe, but don't. Users don't expect that, and doing anything contrary to what users expect irritates them. Research the "onblur" event handler and the "focus()" method. It might be possible to add something to your body tag:

<body onblur="self.focus();">

I haven't tested that, you'll need to experiment.

3) Will this script work in all browsers, or will I have to provide another link directly to the download for certain browsers?
All modern browsers, yes.

To further enhance your script, research JavaScript arrays. As each link is clicked, you could store it's ID in an array. Then, check to see if the current ID (x.id) is alreadly in the array. If it is, they've already clicked that link. Change the text to read "please click a different link" or something like that.

You can also remove links they've already clicked. Check into "CSS visibility".
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the HTML and CSS Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC