Forum Moderators: open

Message Too Old, No Replies

Opening new window not working on Mac, Safari

Replaced target="_blank" with rel="external"

         

SilverLining

10:14 am on Sep 21, 2006 (gmt 0)

10+ Year Member



I have a few external links (inside an IFrame) which I have set target="_blank" on the anchor tags. These links do not open in new windows in Safari.

The next step was to be more Standards-Compliant and so I replace target="_blank" with rel="external" and then called some JavaScript for the external links. JavaScript code below:


function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("href") &&
anchor.getAttribute("rel") == "external")
anchor.target = "_blank";
}
}
window.onload = externalLinks;

This works in IE and Firefox. Any ideas how to get this working on Safari, or what is the correct way of opening links in new windows?

SilverLining

10:27 am on Sep 21, 2006 (gmt 0)

10+ Year Member



Just to expand on this... It also does not work in Firefox on the Mac. The JavaScript file is called from within the head section of the main page and I have tried copying the external JavaScript code into the page (as I'm aware that JS sometimes only works when it is on the page, instead of in a seperate file) and that doesn't work either. Comments appreciated.

kaled

11:49 am on Sep 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using the standard array Links instead of calling getElementsByTagName(). However, I doubt that is the cause of the problem.

You need to break up the code and use alert() to find out where it's going wrong. However, if Safari supports it, try either turning on error reporting or opening a javscript console to (view error messages). I've never used Safari but it should support one of these.

It's also possible that the code is fine but you need to change a setting on Safari.

It might even be doctype-related, maybe.

Kaled.

penders

12:08 pm on Sep 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could try changing:
anchor.target = "_blank";

To:

anchor.setAttribute("target","_blank");

Just a thought?!

spander

12:10 pm on Sep 21, 2006 (gmt 0)

10+ Year Member



target="_blank" works fine for me on a Mac in both Safari and Firefox, and it always has. Must be something local to your system.

SilverLining

10:57 am on Sep 22, 2006 (gmt 0)

10+ Year Member



Thanks for all your responses. Neither of the suggestions worked, however I played around with the JS in the header sections and today it's fixed. Sometimes it's good to sleep on "stuff"...