0

I want to redirect all links on one site to another domain. Except few menu links and one image in the site.. All I could find was this code, and it just disables all links any ideas

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

i want to show tons of demo templates that people can download , but those templates have all sorts of links that i dont want people to surf to , so i want to put one image inside the template saying Look at more templates. and thats the only link i want enabled

user2014557
  • 35
  • 1
  • 8
  • Which link you don't want to disable? – Zorayr Jan 27 '13 at 01:01
  • 1
    well, just put an if statement there and skip the ones you don't want to touch? – eis Jan 27 '13 at 01:02
  • 2
    Why? This sounds like some pretty malicious ponzi scheme type garbage – Cory Danielson Jan 27 '13 at 01:02
  • What did you try? What are you having trouble with? – SLaks Jan 27 '13 at 01:02
  • i want to show tons of demo templates that people can download , but those templates have all sorts of links that i dont want people to surf to , so i want to put one image inside the template saying Look at more templates. and thats the only link i want enabled. – user2014557 Jan 27 '13 at 01:15

3 Answers3

0

You'll need to find some attribute of the links you do not want disabled, and then check for that while looping. For instance, if the image and menu links have # as their href attribute:

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;

It may be worth mentioning that disabling/redirecting a bunch of links without a good reason is a great way to piss off your users and make them never return to your site.

jbabey
  • 45,965
  • 12
  • 71
  • 94
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>
Community
  • 1
  • 1
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • this is a pretty good javascript, but its not working, all the links are disabled, can you take a look at the code again... p.s. im putting this in the head of the page – user2014557 Jan 27 '13 at 01:23
  • If you are using it you changed the `good_links` stuff correct? Post what you have here so it can be debugged. – Giacomo1968 Jan 27 '13 at 01:25
  • – user2014557 Jan 27 '13 at 01:31
  • 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; – user2014557 Jan 27 '13 at 01:32
  • See my edits above including a debugged version of my original script plus a whole HTML file that shows the script in action. – Giacomo1968 Jan 27 '13 at 01:35
  • 1
    Thanks for the response , it worked with the editing option ,, you forgot the //]]> at the end of the code ,, Thanks for all your help. – user2014557 Jan 27 '13 at 01:46
  • Do you mean change the URLs of all links to another site? Sure. Just change the `links[i].href="javascript:return false";` So the value is something you want like `links[i].href="http://www.google.com/";`. That specific line is what changes the `` tags to a new URL. In the example posted it just neutralizes all of them with `javascript:return false` but it can be any valid URL. – Giacomo1968 Jan 27 '13 at 01:56
  • wow , thanks .. maybe one more thing and i will not use more of your time , can i make it browse back ,, something like this – user2014557 Jan 27 '13 at 02:03
  • I’ve already answered your original post & even explained how the replacement works so you can experiment. So if you want to try `javascript:history.back()` I explained how you can try that on your own & replace the string. Experiment. – Giacomo1968 Jan 27 '13 at 02:09
  • Oki, no problem. Thanks for all your help man. Saved me alot of time since im not familiar with javascript. – user2014557 Jan 27 '13 at 02:11
-1

If you're using Apache, and you can differentiate between links to redirect and those not to, you could adapt an Apache mod_rewrite rule...

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

The (.*) would have to change, but it's a start.

Ian Atkin
  • 6,302
  • 2
  • 17
  • 24
  • I dont have access to the apache, im not the hoster. – user2014557 Jan 27 '13 at 01:05
  • thanks for the response , but i dont have access to the apache. – user2014557 Jan 27 '13 at 01:08
  • He wants to disable links on a page that exists but retain a few. Why? Who knows. But `.htaccess` would have to account for that as well if he did have access to change that. – Giacomo1968 Jan 27 '13 at 01:09
  • i have access to the .htaccess file, but as of now they are htaccess.txt and to change that i need to contact my hosting service and ask them to enable something i cant remember now, so im trying to find an alternate way to disable all links except a few of them. but thanks for the response – user2014557 Jan 27 '13 at 01:28