My Webpage

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.

You can loop over document.links, which is a collection of all links in a page, and add a listener to prevent navigation.

@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.

5 Answers 5

$('a').bind("click.myDisable", function() < return false; >); 

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

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 $("", attrs).append($(this).contents()); >); >; >)(jQuery); $('a').changeElementType('span'); 

old (not working well):

var links = document.getElementsByTagName('a'); for(var i=0;i; > 

Источник

I need a pure Javascript code to disable all links while loading the page, avoiding to click them and redirecting to other page when its using a Facebox. Can be something that I place in a tag in the . Any suggestions? EDIT: Ok, I get that in head doesn’t work. But in body ending work properly. That’s why I asked for suggestions. 🙂

Do you just need a script to work on A:href element’s before $(document).ready() (or $(document).load() ) runs?

I think a pure javascript in head will block all links faster then a eventually user click. Thats what I need for now. Waits for jQuery loading appears unnecessarily slow for me.

Читайте также:  Python import matplotlib pyplot as plt

3 Answers 3

Make this the first script tag in your head section:

This catches all click events, and if $ is undefined, cancels the event.

Has the advantage of working even before the DOM is loaded.

In some cases (as I tested) this script doesn’t work in head . But as you said before, @alex script work in body ending. So thx anyway.

I found the code above also blocks out input type=»file» selections. Is there a simple way to exclude those?

Use document.links and prevent their default behaviour with preventDefault() .

var links = document.links; for (var i = 0, length = links.length; i < length; i++) < links[i].onclick = function(e) < e = e || window.event; e.preventDefault(); >> 

If you wanted a more permanent disabling of the links, use removeAttribute(‘href’) .

Place this code somewhere after the closing body tag if you don’t want to use DOMContentLoaded or the window load event.

You can’t place it in the head element otherwise and have it execute immediately because document.links will most certainly be empty.

Источник

EDIT: The original error was a typo. I will rewrite it so it stays useful to the community. How to disable all links with a certain class (e.g. «.disabled») from linking without adding a handler to each element? The advantages of this approach is that it would work with every link (or element) with this class no matter if they were added dinamically or changed their classes during the life cycle of the webpage. It is acceptable (for this use case) if the solution stops the «click» event from bubbling. It is not acceptable (for this use case) to remove the href attribute, it might be «not disabled» later.

Your body one won’t work because the handler for the a tags or any element you actually clicked on would propagate back up to the body. Not down from the body.

Читайте также:  Html header status code

@ste2425 I dont understand your comment. Supposedly any click event in an element bubbles up to body unless stopped. I mean, the handler I wrote gets executed on click. But it opens the link afterwards nevertheless.

Your interpretation of the $(«a»).on(«click», . ) isn’t accurate. jQuery uses event delegation so you aren’t getting one handler per link. Rather, you get one handler that can take care of all links.

one handler per link? please. Alas, as you realized, event delegation does not help at all in this case. Neutering the links individually seems like the only way to go.

@DanielParejoMuñoz, $(document).on(«click», «.linkClass», function(ev)); for dynamically created links..

Источник

Javascript , is there a way to make the javascript disable all links except one

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

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.

3 Answers 3

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 :

  

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

      http://www.google.com/ 
http://www.apple.com/
http://www.yahoo.com/
http://www.catmoji.com/
http://www.bing.com/
http://www.stackoverflow.com/

Источник

What do you mean by «disable»? I assume you don’t want the user to be able to click on it and navigate to its target. Do you also need to prevent right-click,»Open in new Window/Tab»?

Читайте также:  Server dll for css

9 Answers 9

I think the most user-friendly approach is to hide the link. In your button click handler do:

document.getElementById('anchorID').style.visibility = 'hidden'; 
document.getElementById('anchorID').style.visibility = 'visible'; 

Use an onclick=»this.onclick=function()» attribute on the a tag. If there’s a lot of buttons, you should iterate through them in a JavaScript script that adds an event listener for click that is a function that returns false.

function disableAnchor(obj, disable) < if (disable) < var href = obj.getAttribute("href"); if (href && href != "" && href != null) < obj.setAttribute('href_bak', href); >obj.removeAttribute('href'); obj.style.color="gray"; > else < obj.setAttribute('href', obj.attributes['href_bak'].nodeValue); obj.style.color="blue"; >> 
var Link_Enabled_Flag = false; // disable links - background process changes this to true when it's done function Check_Link_Enabled()

IE and Firefox compatible javascript to enable or disable an anchor tag

onclick="disableAnchor(this,'verify')" function disableAnchor(Check_Obj, Check_Id) < var Anchor_id = 's'; thisCheckbox = document.getElementById(Check_Id); thisAnchor = document.getElementById(Anchor_id); if(thisCheckbox.checked)< //alert('Y1'); Check_Obj.setAttribute('href',''); //Check_Obj.attributes['href_bak'].nodeValue Check_Obj.style.color="blue"; //alert('Y2'); >else < //alert('N1'); var href = Check_Obj.getAttribute('href'); //alert(href); if(href && href != "" && href != null)< Check_Obj.setAttribute('href_bak', href); >Check_Obj.removeAttribute('href'); Check_Obj.style.color="gray"; //alert('N2'); > > 

Add an id attribute to the a tag you want to disable, then:

document.getElementById('the_id').href = '#'; 
 $('sub-heading').attr("disabled", "true"); 

The selector is incorrect. For the sample HTML, it should be $(‘.sub-heading a’).attr(«disabled», «true»)

Remove the attribute href like:

I find this very elegant, and it will also make links work only javascript is enabled. In other words, links are disabled by default.

The «disabled» attribute does not stop click on a tags. But I had a A tag designed has a button, and at least, adding «disabled»: true the button was styled has a real disabled button.

Now if you want to stop the click you can simply use the css property «pointer-events: none»

So you can use something like this :

$('.sub-heading a').attr("disabled", "true"); $('.sub-heading a').css('pointer-events', 'none'); 

If you want to do it through clicking on other button :

jQuery('.my-other-button').on('click', function() < $('.sub-heading a').attr("disabled", "true"); $('.sub-heading a').css('pointer-events', 'none'); >); 

Источник

Оцените статью