0

I've a script (an ads one) and I'd like to to trigger him out only when a button on my site has been clicked for X times.

<script type="text/javascript" src="ADS_URL"></script>

Let's assume this is my script. This is what I've done to find when the button has been clicked 5 times, but after it I'm blocked. I don't know how to trigger the script out.

Where should I paste the script? Thank you for the help!

$("#button").click(function() {
    nclick++;
    if (nclick == 7) {
        nclick = 0;
        // Make something
    };
});
ene
  • 75
  • 1
  • 11

3 Answers3

1

The following code works fine for me:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Example</title>
    <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
    <script>
"use strict";
var nclick = 0;
$(document).ready(function() {
// ===== START OF QUESTION CODE =====
  $("#button").click(function() {
    nclick++;
    if (nclick == 7) {
      nclick = 0;
      alert("Pretend that this is an ad.");
    }
  });
// ===== END OF QUESTION CODE =====
});
    </script>
  </head>
  <body>
    <a id="button" href="#">Click me!</a>
  </body>
</html>

If your code is not running:

  • Check the browser console. Are there any errors?
  • Is jQuery included on your page?
  • Are you attaching the #button function when the page is ready? You might be attempting to add an event before the button is created.
  • Have you declared an nclick variable and initialised it to zero?

EDIT

Okay, the question has now been clarified - the ad script appears as soon as the page is loaded, but you want to delay it until a button has been clicked 7 times. The solution is basically the same as the above, though:

ads.js

alert("hello, world!");

example.htm

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Example</title>
    <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
    <script>
"use strict";
var nclick = 0;
var ad_script = null;
$(document).ready(function() {
// ===== START OF QUESTION CODE =====
  $("#button").click(function() {
    nclick++;
    if (nclick == 7) {
      nclick = 0;
      ad_script = document.body.appendChild(document.createElement("script"));
      ad_script.src = "ads.js"
    }
    else if (ad_script)
    {
      document.body.removeChild(ad_script);    
      ad_script = null;
    }
  });
// ===== END OF QUESTION CODE =====
});
    </script>
  </head>
  <body>
    <a id="button" href="#">Click me!</a>
  </body>
</html>

Simply add the script into the page once the counter reaches 7.

GoBusto
  • 4,632
  • 6
  • 28
  • 45
  • Maybe I've not explained my problem well. My problem is: if I paste the ADS script in the head, the ads are printed when users open the site. I want to print it out when a btn has been clicked for 7 times! I miss the "Pretend that is an ad" part. Where should I paste my AD-script? – ene Feb 13 '15 at 09:47
  • I'd suggest that you update your question text to emphasize this, because that's not how I interpreted it. – GoBusto Feb 13 '15 at 09:48
  • Thank you @GoBusto! So all I've to do is put my script in a file called ads.js, right? – ene Feb 13 '15 at 09:57
  • You could, though I presume that you'd simply want to use `ADS_URL` directly: `document.body.appendChild(document.createElement("script")).src = ADS_URL;` – GoBusto Feb 13 '15 at 10:00
  • Ok, the only "problem" is that I've to remove the script. I want to print it every time a user press for 7 times the button. http://pastebin.com/RNWB6ZCU Is this right? Assume that cScript is create script (from a previous answer) – ene Feb 13 '15 at 10:06
  • I've updated my answer - hopefully this should do what you want. – GoBusto Feb 13 '15 at 10:11
  • Thank you! It is! :) It gives me an error back, but I assume that is an ad-problem. PS: should I put something like ad_script.type = "text/javascript"? – ene Feb 13 '15 at 10:24
  • [No - It's not required in HTML5.](http://www.w3.org/TR/html5/scripting-1.html#attr-script-type) – GoBusto Feb 13 '15 at 10:26
1

create script tag & remove script tag

cScript();
var nclick=0;
$("#button").click(function() {
nclick++;

if (nclick == 7) {
    rScript();
    nclick = 0;
    // Make something
};
});

function cScript(){
var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = 'ADS_URL';
$("#someElement").append( script );
}

function rScript(){
var html = $("#someElement");
html.find('script').remove();
}
Community
  • 1
  • 1
jupeter
  • 746
  • 4
  • 18
  • Thank you, this looks cool! (I've tried yet, because I've some quesitons) If you call cScript() in the first line won't it print the script as soon as page is load? I want to print the ads only when button has been clicked 7 times! Maybe I could make something like this. Code > http://pastebin.com/RNWB6ZCU Am I wrong? – ene Feb 13 '15 at 10:03
0

If i understood you correctly your JQuery event is not getting executed?

You need to add the JQuery's source first:

  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js? ver=1.4.2"></script>

    <script type="text/javascript" src="ADS_URL"></script>
 </head>

If you don't have the source for JQuery included it won't know what you mean. with $("#button").click

dHoja
  • 206
  • 3
  • 10
  • I've not explained my problem well. Shame on me. Btw, my problem is: if I paste the ADS script in the head, the ads are printed when users open the site. I want to print it out when a btn has been clicked for 7 times! – ene Feb 13 '15 at 09:46
  • Does the ADS_URL scipt contain a function or variables that holds the content that you wan't to display? Or does the content get displayed automaticly? – dHoja Feb 13 '15 at 09:55