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

javascript - Disable all hyperlinks in any website - Stack Overflow

programmeradmin5浏览0评论

Is there a way to disable all hyperlinks in any website using javascript?

The reason I want to do this is for text-selection. Many websites are putting whole blocks of text inside tags, making it difficult to select text.

I use chromium and firefox on kubuntu 13.10.

Is there a way to disable all hyperlinks in any website using javascript?

The reason I want to do this is for text-selection. Many websites are putting whole blocks of text inside tags, making it difficult to select text.

I use chromium and firefox on kubuntu 13.10.

Share Improve this question asked Apr 21, 2014 at 8:14 qedqed 23.1k25 gold badges131 silver badges210 bronze badges 3
  • You can loop over document.links, which is a collection of all links in a page, and add a listener to prevent navigation. – RobG Commented Apr 21, 2014 at 8:22
  • Alternatively, just remove the href attribute so they are no longer links. – RobG Commented Apr 21, 2014 at 10:38
  • @RobG Could you please test this trick on nos.nl ? On the left side, after all href attributes are removed, some blocks still behave as if they are links. – qed Commented Apr 21, 2014 at 11:55
Add a comment  | 

5 Answers 5

Reset to default 8

To disable all links use

$('a').bind("click.myDisable", function() { 
   return false; 
});

and to enable it use

$('a').unbind("click.myDisable");

Yes it is. You can use Greasemonkey addon for Firefox and Tampermonkey for Chrome to achieve this.

UPDATE

use this in greasemonkey. it will replace all links with spans. this way they can be selected like normal text:

// @require       http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.js

(function($) {
    $.fn.changeElementType = function(newType) {
        var attrs = {};

        $.each(this[0].attributes, function(idx, attr) {
            attrs[attr.nodeName] = attr.nodeValue;
        });

        this.replaceWith(function() {
            return $("<" + newType + "/>", attrs).append($(this).contents());
        });
    };
})(jQuery);

$('a').changeElementType('span');

old (not working well):

the script could be:

var links = document.getElementsByTagName('a');
for(var i=0;i<links.length;i++)
{
  links[i].href='#';
  links[i].onclick = function(){  return false; };
}

Okay completely different answer to my deleted answer using what Rob G said.

for (var x=document.links.length-1;x>=0;x--) {
document.links[x].removeAttribute("href")
}
function replaceA(element) {
    for (var i=element.children.length-1;i>=0;i--) {
        var getChild = element.children[i];
        if (getChild.tagName == "A") {
            element.innerHTML = element.innerHTML.replace(getChild.outerHTML,getChild.innerHTML);
        } else {
            replaceA(getChild);
        }
    }
}

window.onload = function(){replaceA(document.body)};

A bit hacky, basically when page has loaded, it will replace all links with just their innerHTML text. If you wanna use it instead of when the page loads, just remove the window.onload part, and call replaceA(document.body) when you wish to use it

According to this forum thread, there are some options:

  1. I usually paste it into 'TextEdit' and making sure that TexEdit is displaying fonts as Plain Text. Then copy the links or texts from TexEdit and paste it into Pages.
  2. Open the Inspector palette. Select the second last tab called "Link Inspector", then highlight the link itself. Click the "Enable as a hyperlink" checkbox to off and it will disappear.
  3. In Pages preferences, there's an option on the "Auto Correct" tab to "Automatically detect email and web addresses". Untick that and it shouldn't happen if what you're pasting contains a URL or an email address.

Hope that helps.

发布评论

评论列表(0)

  1. 暂无评论