最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to run a script when clicking on a link? - Stack Overflow

programmeradmin1浏览0评论

So, I have a javascript for a popunder ad, (yeah I know they're annoying but it's not an ad, it's a page that opens behind the main page) anyway, because the script has the page open automatically when you go to my page, pop up blockers automatically block it, so what I would like to do is have the script run when someone clicks a link on my page, therefore it is user initiated and not automatically, so it doesn't get blocked by a popup blocker.. basically, so when someone clicks on one of the links on my page, it opens the link, but also opens the pop under behind my page.. I'm kind of new to javascript, but any help is appreciated!

So, I have a javascript for a popunder ad, (yeah I know they're annoying but it's not an ad, it's a page that opens behind the main page) anyway, because the script has the page open automatically when you go to my page, pop up blockers automatically block it, so what I would like to do is have the script run when someone clicks a link on my page, therefore it is user initiated and not automatically, so it doesn't get blocked by a popup blocker.. basically, so when someone clicks on one of the links on my page, it opens the link, but also opens the pop under behind my page.. I'm kind of new to javascript, but any help is appreciated!

Share Improve this question edited Jul 3, 2013 at 7:07 AntouanK 4,9881 gold badge23 silver badges26 bronze badges asked Jul 3, 2013 at 7:01 BrendanMann_1BrendanMann_1 111 gold badge1 silver badge2 bronze badges 2
  • 2 Whats the onClick event for? – Mr. Alien Commented Jul 3, 2013 at 7:02
  • There isn't an onClick event, atleast I don't think so... 2createawebsite./enhance/create-popunder.html is where I got the script from, so if you see anything in there that I didn't it would be really helpful – BrendanMann_1 Commented Jul 3, 2013 at 7:06
Add a ment  | 

4 Answers 4

Reset to default 4

This would be one approach:

HTML:

<a id="link">Link</a>

JavaScript:

function script() {
    alert("I'm the ad");
};

document.getElementById('link').onclick = function () {
    script();
};

For demonstration see this Fiddle.

/Edit: Sure, here is the JavaScript:

// copy and paste the script from the website

document.getElementById('open').onclick = function () {
    load_pop_power();
};

If you don't want to show the ad when the user visits the site, you could however delete half of the code.

You could do something like this

function doSomething() {
    //do your actions here
    window.location="http://www.gotothelink.";
}
</script>
<a href="javascript:doSomething();">click me</a>

When the user clicks the link, the javascript code in the "onlick" attribute is executed. "window.open" opens a new window and "return true" has the effekt that the normal behaviour of the link keeps working.

<a href="http://www.tiscover." onclick="window.open('http://www.tiscover.'); return true;">tiscover</a>

So you need to bind the popunder to a click event on a link see http://www.pagecolumn./javascript/bind_event_in_js_object.htm

发布评论

评论列表(0)

  1. 暂无评论