0

I'm working on a project where I need to temporarily disable all hyperlinks, and then enable them again once my pop-up div is gone. I am able to successfully disable all the links using this function I wrote:

    function disableHyperlinks(){
        link_targets = Array();
        var anchors = document.getElementsByTagName("a");
        for(var i = 0; i < anchors.length; i++){
            link_targets.push(anchors[i].href);
            anchors[i].href= "#";
        }
     }

Which also saves all the URLs so it can put them back later using this function:

    function enableHyperlinks(){
        var anchors = document.getElementsByTagName("a");
        for(var i = 0; i < anchors.length; i++){
            anchors[i].href= link_targets[i];
        }
    }

The above code seems to work just fine, it removes all the links, and then puts them all back without any issues, however the problem is that if I run the 'enable' code after a link is clicked, its almost as if the javascript is setting the link back to the original destination and then registering the click. So despite being 'disabled' in this fashion, the link still ends up leaving the page.

The problem is demonstrated here

Click the red "L" with the white background to enable the javascript I made for selection, you'll notice anything you bring your mouse over will get a blue dashed border, I need to be able to "select" parts of the web page without redirecting to another page if a link is also clicked.. any idea how I could go about doing this properly?

(Please note I am trying to avoid JQuery)

1
  • Alright, I caved, I'm using JQuery now. Was able to solve this issue in Chrome using preventDefault(); but that doesn't fix the problem in Firefox. Commented Jul 11, 2012 at 20:34

3 Answers 3

2

Work on the onClick listener:

var areEnabled = false;
function toggleLinks(){
    var anchors = document.getElementsByTagName('a');
    for(var i=0; i < anchors.length; i++)
        anchors[i].onclick = (areEnabled) ? null : function(e){ return false };
    areEnabled = !areEnabled;
};
Sign up to request clarification or add additional context in comments.

3 Comments

I wasn't aware the <a> tag had that property, thanks! However, using that method did not fix the problem.
should be onclick, not onClick
Still doesn't seem to be working, I think the issue might not be how exactly I'm disabling the links. When I hover over the "disabled" link it does indeed show that it goes to "#", but then when I click it, it goes to where it was pointing originally instead of "#". o_O Link to JS: fullofepicwin.com/likeler/StackOverflowCopy/imsafe/likeler.js
0

Instead of searching for many links and disabling them (which, BTW, won't help you against other clickable objects), you can create another invisible div, just below your popup on z-index that would cover entire page and catch all clicks instead of elements underneath. You can even make it semi-transparent instead of completely invisible for visual effect alerting user that "lower-level" is disabled.

3 Comments

Please look at the demo site I linked to, I need to be able to select items on the page, and click them, without clicking the links. I'm not worried about clickable items behind the popup div.
@PatrickTNelson, indeed. How about then same solution with covering everything in div, assigning it the handler that would determine element under mouse and either let click even fall through or cancel it?
Sounds good, but I'm somewhat of a novice when it comes to web dev, I'd have no clue how to implement that. Have a link to a tutorial of some form that could be helpful?
0

I was finally able to solve my own problem by manipulating the onclick property of each hyperlink, then setting it back to what it was previously. The only problem with bokonic's response was that the onclick property was set to null to "re-enable" the link, which would then disable any javascript functionality the link had previously.

var onclickEvents;

function disableHyperlinks(){
    onclickEvents = Array();
    var anchors = document.getElementsByTagName("a");
    for(var i = 0; i < anchors.length; i++){
        onclickEvents.push(anchors[i].onclick);
        anchors[i].onclick=function(){return false;};
    }
}

function enableHyperlinks(){
    var anchors = document.getElementsByTagName("a");
    for(var i = 0; i < anchors.length; i++){
        anchors[i].onclick = onclickEvents[i];
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.