View Full Version : Open Offsite Links in New Window

12-02-2007, 11:27 PM
1) Script Title: Offsite Links in new Window

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex8/newwindow2.htm

3) Describe problem: I have got this working great at the moment, so I am very happy. But just with further work on my page, I need an extra feature. I may not have set it up correctly, but what I require is not just the main domain to be excluded, but extra directories off it. That is, I want links to open in a new window from www.mydomain.com and www.mydomain.com/myfirst/, but not from www.mydomain.com/myfirst/mysecond/. At this stage, this is the only one I want excluded, if this makes it any easier.

Thanks in advance.

12-03-2007, 07:45 AM
As you start to get into fine tuning this script along the lines that you are describing, it becomes increasingly more sensible to simply manually add the target attribute to any link that may (in your scheme of things) require it.

12-03-2007, 10:33 PM
Thanks for the reply.

I have tried putting a target="(_parent, _self, _top)" on the links that I want to always open in the same window, no matter what the check box says. Unfortunately didn't work.

Do I need to change the script in some way so that it would look for this target attribute first?

Or am I missing something?

Thanks again.

12-03-2007, 10:48 PM
Further testing has shown that even if I place a target="_blank" on a link, if the checkbox is unticked, every link loads in the same window. That is, there doesn't seem to be an override.

12-04-2007, 10:39 AM
I meant abandon the script and use the target attributes instead.

12-14-2007, 06:40 PM
I was able to solve this problem.

I put
target="_top" on the links that I never want to open in a new window, then adjusted the script as such [adding in the red section]:

if (all_links[i].hostname.search(rexcludedomains)==-1 && all_links[i].href.indexOf("http:")!=-1 && all_links[i].target!="_top")

I don't know how robust this solution is, but it works for my purpose.

12-14-2007, 06:48 PM
There's more than one way to skin a cat. Aside from the target attribute being deprecated, that's as good a solution as any. My idea would have required use of target too. A script could be written to assign an event to these links that wouldn't involve the target attribute at all. A valid flag like a class name could be used to exempt links.

I think what you did is fine though. The advantage with my idea of abandoning the script was that non-javascript enabled browsers would be able to follow it. The advantage with my idea for a new script is that it would be technically valid, more 'future proof'.