To protect the ads, I thought of creating a protective layer, that is, a div
transparent one that activates after there are two or three clicks
given by the same user and thus avoid more clicks
from the same visitor.
But, of course, this can't be enough, so in addition to creating a layer, I would have to delete all the links that the link(s) contains iframe
and iframe
that my container has <div class="ads"></div>
.
$(document).ready(function() {
$(".ads iframe").load(function() {
$(".layer-protect").hide();
});
});
.ads {
position: relative;
}
.layer-protect {
position: absolute;
}
iframe {
position: absolute;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="ads">
<iframe src="https://es.stackoverflow.com/"></iframe>
</div>
<div class="layer-protect">
<p>Hi! Testing...</p>
</div>
iframe
I cannot modify the links within a , as you will already know that it is impossible.
The idea is to only allow the user to click on those ads only two or three times, for this you must create a type of click counter and store them in localStorage
since it is not recommended to use cookie
it because it can overload my server.
Storage localStorage
must be secure and last for a limited time of 24 or 48 hours, after that time that data is deleted, it is important that this expiration function works or not for the online user.
At the moment I have the following jQuery code:
let i = 0;
$("iframe *").each(function() {
this.pos = i;
$(this).on("click", (e) => {
if (localStorage.getItem("link"+i) == null) {
localStorage.setItem("link"+i, 1);
} else {
let clicks = localStorage.getItem("link"+i);
if (clicks >= 3) {
if (typeof e.target.getAttribute("href") == "string" && e.target.getAttribute("href").toLowerCase().search(location.hostname.toString().toLowerCase()) == -1) {
e.preventDefault();
}
} else {
localStorage.removeItem("link"+i);
clicks++;
localStorage.setItem("link"+i, clicks);
}
}
});
i++;
});
But it doesn't work, I've tried creating a page with the following structure:
<div>
<a href="https://example.com" target="_black">1</a>
<a href="https://example.com" target="_black">2</a>
<a href="https://example.com" target="_black">3</a>
<a href="https://example.com" target="_black">4</a>
<a href="https://example.com" target="_black">5</a>
<a href="https://example.com" target="_black">6</a>
<a href="https://example.com" target="_black">7</a>
<a href="https://example.com" target="_black">8</a>
</div>
And, I have wrapped/called it in a iframe
giving more than 4 clicks on said links, but they have not been removed or disabled.
How can I correctly employ this problem? Can you explain me how to solve it.
I have been looking at some references: