Here's an example of a floating click-through ad that appears on the bottom-right corner of the page

<!-- HTML code for the ad -->
<div id="ad-container" style="position: fixed; bottom: 0; right: 0; width: 200px; height: 200px;">
  <a href="https://www.example.com/ad-page" id="ad-link" aria-label="Advertisement" target="_blank">
    <img src="/ad-image.jpg" alt="Advertisement" style="max-width: 100%; max-height: 100%;">
  </a>
</div>

<!-- JavaScript code for the ad -->
<script>
  // Get a reference to the ad link and container
  var adLink = document.getElementById('ad-link');
  var adContainer = document.getElementById('ad-container');

  // Add a click event listener to the ad link
  adLink.addEventListener('click', function(event) {
    // Track the click using analytics code or ad platform code
    // ...

    // Open the ad destination URL in a new tab
    var adUrl = adLink.href;
    window.open(adUrl, '_blank');

    // Prevent the default behavior of the link
    event.preventDefault();
  });

  // Hide the ad container after 10 seconds
  setTimeout(function() {
    adContainer.style.display = 'none';
  }, 10000);
</script>

In this example, we're using the same click-through ad code as before, but we're wrapping it inside a div element with an id of ad-container. We're also using inline styles to position the container at the bottom-right corner of the page, and setting its width and height to 200px.

The JavaScript code is similar to before, but we're also adding a setTimeout() function to hide the ad container after 10 seconds. This is useful to prevent the ad from being too intrusive or annoying to users.

Note that the exact positioning and styling of the ad container may vary depending on your website's layout and design. You can adjust the bottom, right, width, and height values of the style attribute to position the ad container where you want it, and adjust the max-width and max-height values of the style attribute for the img element to ensure the ad image fits within the container.

Here's a more complete example of a click-through ad script

Here's a more complete example of a click-through ad script that includes some best practices for tracking clicks and ensuring the ad is accessible:

<!-- HTML code for the ad -->
<a href="https://www.example.com/ad-page" id="ad-link" aria-label="Advertisement" target="_blank">
  <img src="/ad-image.jpg" alt="Advertisement">
</a>

<!-- JavaScript code for the ad -->
<script>
  // Get a reference to the ad link
  var adLink = document.getElementById('ad-link');

  // Add a click event listener to the ad link
  adLink.addEventListener('click', function(event) {
    // Track the click using analytics code or ad platform code
    // ...

    // Open the ad destination URL in a new tab
    var adUrl = adLink.href;
    window.open(adUrl, '_blank');

    // Prevent the default behavior of the link
    event.preventDefault();
  });
</script>

Let's break down the code:

  • The a element includes an href attribute with the URL of the ad destination, an id attribute for targeting the element with JavaScript, an aria-label attribute for accessibility, and a target="_blank" attribute to open the ad in a new tab. The img element includes an alt attribute for accessibility.

  • The JavaScript code gets a reference to the ad link using document.getElementById(), and adds a click event listener to it using addEventListener(). Inside the event listener, you can add tracking code to track the ad click, either using analytics code or ad platform code.

  • The window.open() method opens the ad destination URL in a new tab, using the _blank value for the target parameter. This ensures that the user stays on your website while the ad opens in a new tab.

  • Finally, we prevent the default behavior of the link using event.preventDefault(), to avoid any unintended actions from the user clicking on the ad.

Here's a more compact version of the click-through ad script using jQuery

<!-- HTML code for the ad -->
<a href="#" id="ad-link"><img src="/ad-image.jpg" alt="Advertisement"></a>

<!-- jQuery code for the ad -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
  $(document).ready(function() {
    // Add a click event listener to the ad link
    $('#ad-link').click(function(event) {
      // Prevent the default behavior of the link
      event.preventDefault();

      // Redirect the user to the ad destination URL
      window.location.href = 'https://www.example.com/ad-page';
    });
  });
</script>

In this example, we're using the jQuery library to simplify the code. The $(document).ready() function ensures that the code runs only after the page has finished loading.

The $() function is a shorthand for jQuery(), which allows us to select the ad link using its ID (#ad-link). We then add a click event listener to the selected element using the .click() method.

Inside the click event listener, we prevent the default behavior of the link using event.preventDefault(), and then redirect the user to the ad destination URL using window.location.href.

Here's a simple example of a click-through ad script using JavaScript

<!-- HTML code for the ad -->
<a href="#" id="ad-link">
  <img src="/ad-image.jpg" alt="Advertisement">
</a>

<!-- JavaScript code for the ad -->
<script>
  // Get a reference to the ad link
  var adLink = document.getElementById('ad-link');

  // Add a click event listener to the ad link
  adLink.addEventListener('click', function(event) {
    // Prevent the default behavior of the link
    event.preventDefault();

    // Redirect the user to the ad destination URL
    window.location.href = 'https://www.example.com/ad-page';
  });
</script>

Replace the ad-image.jpg with the image file you want to use for the ad, and change the https://www.example.com/ad-page URL to the actual destination URL of your ad.

Note that some ad platforms have their own click tracking mechanisms, so you may need to use their provided code instead of the example above.

- All From ChatGPT
PLG_GSPEECH_SPEECH_BLOCK_TITLE