在链接上运行功能,将用户发送到预链接广告页面,广告页面在5秒后重定向用户

时间:2016-12-23 00:58:15

标签: javascript jquery

在页面上有一堆像这样的链接:

<a class="wsite-button wsite-button-large wsite-button-highlight"  href="/uploads/7/0/9/4/70947887/custom_themes/436120861689278040/files/Games/17waystokillboss.swf" id="link" onclick="doalert(); return false;><span class="wsite-button-inner">17 Ways To Kill Your Boss(NEW)</a><br>
<a class="wsite-button wsite-button-large wsite-button-highlight"  href="/uploads/7/0/9/4/70947887/custom_themes/436120861689278040/files/Games/24waystokyb.swf" id="link" onclick="doalert(); return false;><span class="wsite-button-inner">24 Ways To Kill Your Boss(NEW)</a><br>
<a class="wsite-button wsite-button-large wsite-button-highlight"  href="/uploads/7/0/9/4/70947887/custom_themes/436120861689278040/files/Games/4-wheel-madness.swf" id="link" onclick="doalert(); return false;><span class="wsite-button-inner">4 Wheel Madness</a><br> 
<a class="wsite-button wsite-button-large wsite-button-highlight"  href="/uploads/7/0/9/4/70947887/custom_themes/436120861689278040/files/Games/Achilles.swf" id="link" onclick="doalert(); return false;><span class="wsite-button-inner">Achillies(NEW)</a><br>
<a class="wsite-button wsite-button-large wsite-button-highlight"  href="/uploads/7/0/9/4/70947887/custom_themes/436120861689278040/files/Games/alien_hominid.swf" id="link" onclick="doalert(); return false;><span class="wsite-button-inner">Alien Hominid(NEWER)</a><br>
<a class="wsite-button wsite-button-large wsite-button-highlight"  href="/uploads/7/0/9/4/70947887/custom_themes/436120861689278040/files/Games/asteroids.swf" id="link" onclick="doalert(); return false;><span class="wsite-button-inner">Asteriods(NEW)</a><br>

运行此页面以发送到广告页面的脚本:

<script>
function doalert() {
var str1 = "http://www.goldandblack.net/adpage.html#";

    window.location.href = str1.concat(document.getElementById("link").getAttribute("href")));
    return false;
};
</script>

广告上的脚本转发到点击的页面:

<script>
window.onload = function() {
var str2 = window.location.href;
var str3 = str2.replace("http://www.goldandblack.net/adpage.html#", "");
window.location.href = str3;
};

</script>

由于某种原因,这不起作用

1 个答案:

答案 0 :(得分:1)

id属性对于任何元素都应该是唯一的。所以拥有所有元素id="link"都会导致问题。

您可以执行的操作是删除id属性并将元素self发送到doalert()函数,例如doalert(this)

所以你的html部分将是这样的:

<a class="wsite-button wsite-button-large wsite-button-highlight" href="/uploads/7/0/9/4/70947887/custom_themes/436120861689278040/files/Games/17waystokillboss.swf" onclick="doalert(this); return false;"><span class="wsite-button-inner">17 Ways To Kill Your Boss(NEW)</a><br>
<a class="wsite-button wsite-button-large wsite-button-highlight" href="/uploads/7/0/9/4/70947887/custom_themes/436120861689278040/files/Games/24waystokyb.swf" onclick="doalert(this); return false;"><span class="wsite-button-inner">24 Ways To Kill Your Boss(NEW)</a><br>
<a class="wsite-button wsite-button-large wsite-button-highlight" href="/uploads/7/0/9/4/70947887/custom_themes/436120861689278040/files/Games/4-wheel-madness.swf" onclick="doalert(this); return false;"><span class="wsite-button-inner">4 Wheel Madness</a><br> 
<a class="wsite-button wsite-button-large wsite-button-highlight" href="/uploads/7/0/9/4/70947887/custom_themes/436120861689278040/files/Games/Achilles.swf" onclick="doalert(this); return false;"><span class="wsite-button-inner">Achillies(NEW)</a><br>
<a class="wsite-button wsite-button-large wsite-button-highlight" href="/uploads/7/0/9/4/70947887/custom_themes/436120861689278040/files/Games/alien_hominid.swf" onclick="doalert(this); return false;"><span class="wsite-button-inner">Alien Hominid(NEWER)</a><br>
<a class="wsite-button wsite-button-large wsite-button-highlight" href="/uploads/7/0/9/4/70947887/custom_themes/436120861689278040/files/Games/asteroids.swf" onclick="doalert(this); return false;"><span class="wsite-button-inner">Asteriods(NEW)</a><br>

然后更改您的JavaScript函数doalert()并将参数添加到其中:doalert(obj)然后使用obj代替document.getElementById("link")

所以你的JavaScript部分将是这样的:

<script>
function doalert(obj) {
    var str1 = "http://www.goldandblack.net/adpage.html#";
    window.location.href = str1.concat(obj.getAttribute("href"));
    return false;
};
</script>

此外,您可以使用此行获取Location hash Propertywindow.location.hash

然后它将在开头返回带有#的网址的锚点部分。所以我们得到它并在开头删除#字符。

因此script文件中的adpage.html将是这样的:

<script>
window.onload = function() {
    var str3 = window.location.hash;
    str3 = str3.substring(1); // remove the first char from string
    window.location.href = str3;
};
</script>