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
.