0

我想将一个站点上的所有链接重定向到另一个域。除了网站中的几个菜单链接和一张图片..我能找到的只是这段代码,它只是禁用了所有链接的任何想法

function disable() {
  links=document.getElementsByTagName('A');
  for(var i=0; i<links.length; i++) {
    links[i].href="javascript:return false";
  }
}
window.onload=disable;

我想展示大量可供人们下载的演示模板,但这些模板有各种我不希望人们浏览的链接,所以我想在模板中放一张图片,说看看更多模板。这就是我想要启用的唯一链接

4

3 回答 3

0

Using the script you have as an example just set a conditional. I used the inArray() function described here. The basic logic is to set an array called good_links that is a list of links you do not want neutralized. And then by using the inArray() function, the disable() function can now logically traverse all links but ignore the good_links:

<script type="text/javascript">

    function disable(){
        var good_links = new Array();
        good_links[0] = "http://www.google.com/";
        good_links[1] = "http://www.yahoo.com/";
        good_links[2] = "http://www.bing.com/";
        links=document.getElementsByTagName('A');
        for(var i=0; i<links.length; i++) {
            if (!inArray(links[i].href, good_links)) {
                links[i].href="javascript:return false";
            }
        }
    }

    function inArray(needle, haystack) {
        var length = haystack.length;
        for(var i = 0; i < length; i++) {
            if(haystack[i] == needle) return true;
        }
        return false;
    }

    window.onload=disable;

</script>

Okay, here is my whole HTML file which works. The only links that should work are: Google, Yahoo & Bing. Apple, Catmoji & StackOverflow are disabled.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>My Webpage</title>
    <script type="text/javascript">
//<![CDATA[

    function disable(){
        var good_links = new Array();
        good_links[0] = "http://www.google.com/";
        good_links[1] = "http://www.yahoo.com/";
        good_links[2] = "http://www.bing.com/";
        links=document.getElementsByTagName('A');
        for(var i=0; i<links.length; i++) {
            if (!inArray(links[i].href, good_links)) {
                links[i].href="javascript:return false";
            }
        }
    }

    function inArray(needle, haystack) {
        var length = haystack.length;
        for(var i = 0; i < length; i++) {
            if(haystack[i] == needle) return true;
        }
        return false;
    }

    window.onload=disable;

    //]]>
    </script>
</head>

<body>
    <a href="http://www.google.com/">http://www.google.com/</a><br />
    <a href="http://www.apple.com/">http://www.apple.com/</a><br />
    <a href="http://www.yahoo.com/">http://www.yahoo.com/</a><br />
    <a href="http://www.catmoji.com/">http://www.catmoji.com/</a><br />
    <a href="http://www.bing.com/">http://www.bing.com/</a><br />
    <a href="http://www.stackoverflow.com/">http://www.stackoverflow.com/</a>
</body>
</html>
于 2013-01-27T01:08:29.440 回答
0

您需要找到您不想禁用的链接的某些属性,然后在循环时检查该属性。例如,如果图像和菜单链接具有#作为它们的href属性:

function disableLinks () {
    var allLinks=document.getElementsByTagName('A');

    for(var i=0; i<allLinks.length; i++) {
        if (allLinks[i].href !== '#') {
            allLinks[i].href="javascript:return false";
        }
    }
}

window.onload = disableLinks;

值得一提的是,在没有充分理由的情况下禁用/重定向一堆链接是激怒用户并使他们永远不会返回您的网站的好方法。

于 2013-01-27T01:03:53.123 回答
-1

如果您使用的是 Apache,并且可以区分重定向链接和不重定向链接,则可以调整 Apache mod_rewrite 规则...

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

必须改变,(.*)但这是一个开始。

于 2013-01-27T01:03:23.033 回答