dimanche 10 juillet 2016

Javascript open new tab and redirect old one from external script?

What I want to do is, adding this javascript to website www.user.com

<script src='http://network.com/pop.js'></script>

When a visitor clicks, the actual target of this click opens in a new tab, and the current website redirects to some ad. Here is the code which works when I put it at my own site network.com, but when I put it at another website it doesn't work.

document.addEventListener("click", function (e) {


    if (e.target.tagName == "A" && !e.target.hasAttribute("target")) {
        e.target.setAttribute("target", "_blank");
    }
});  

window.onclick = function() {
     location.href = "http://ad.com/etc";
}

Here you can see the working demo at my site: http://keepvisitors.com/adcode2.html

Aucun commentaire:

Enregistrer un commentaire