Jump to content

Allow links to open in new window with on/off feature?


Jason28

Recommended Posts

Hello, could anyone please provide me with a working example of a javascript that detects all of the active urls on the page and opens them in another window but has the option of opening all links normally? (without target="_blank").  So someone can click a link to disable links opening in a new window, but on default they will open in a new window.  Could you please provide a working example?  Thanks.

Link to comment
Share on other sites

Would be easy enough to do using jQuery. Something like....

 

var newwindow = 'on'; // set this via a form or whatever.
$(document).ready(function() {
    $('a').each(function() {
        if (newwindow == 'on') {
            $(this).attr('target', '_blank');
        } else {
            $(this).attr('target', '_self');
        }
    });
});

Link to comment
Share on other sites

Thanks for replying.  Could you please provide me with the html to use along with this javascript?  A link that would change the text like "open links in same window" and when clicked, it changes to "open links in new window"?  Thanks.

Link to comment
Share on other sites

I'm not here to pump out code for people. Take a look at jQuery's toggle method. You could easily apply that to a button which would switch the 'newwindow' var from 'on' to 'off'.

Link to comment
Share on other sites

Well sorry but I tried learning how to do this for many years but I just do not understand it.  If you only give me part of the code I cannot do anything with it.  I understand you do not have to do anything, but this is just a waste of time for us since I cannot do anything with half of a script.  Could anyone else help please?

Link to comment
Share on other sites

but this is just a waste of time for us since I cannot do anything with half of a script.

The script that was posted previously was pretty much the whole thing written out for you except for a button/link that changes the status of a variable.

 

Can you post what you have tried so far? Most likely no one is going to help much  if you don't show have put in a bit of effort yourself

Link to comment
Share on other sites

Well sorry but I tried learning how to do this for many years but I just do not understand it.  If you only give me part of the code I cannot do anything with it.  I understand you do not have to do anything, but this is just a waste of time for us since I cannot do anything with half of a script.  Could anyone else help please?

 

Go hire someone then.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.