问题描述:

I want to open hyperlinks in a div and stop hyperlink navigation.

Ideally this should work for all links on the page "especially when debugger / console is open"

  1. How to detect if the console is open on all browsers?

    window.console, window.console.firebug doe not seem to work with me.

  2. I am setting the innerHTML property of my div right now which opens all the images etc in my div. I want only the text to be streamed. How do I do that? This is my code:

    function load_home(){

    document.getElementById("main").text='<object type="text/html" data="my link path" ></object>';

    }

网友答案:

To stop links from directing you to another page (what I'm assuming you mean by "hyperlink redirection"), use JavaScript to stop them with an event listener and preventDefault(). Try adding this to your code:

var links = document.getElementsByTagName("a");
for(var i = 0; i < links.length; i++) {
    links[i].addEventListener("click", function(event) {
        event.preventDefault();
    });
}

And in that anonymous function in the event listener, you can add the code to load / redirect as wished.

相关阅读:
Top