Open external link in new window

tlynn721

New Member
I'm very new to Javascript and I'm having a hard time finding a comprehensive answer to my issue.

I have 2 websites. When a visitor clicks on the link for my main website, I want it to open in a new window. I've tried several variations but nothing has worked so far. Here is my current script:

window.mm_menu_0204220223_0 = new Menu("root",171,18,"Verdana, Arial, Helvetica, sans-serif",12,"#000000","#333333","#F4F400","#FFFF80","left","middle",3,0,1000,-5,7,true,true,true,0,true,true);
mm_menu_0204220223_0.addMenuItem("Snake Tray Main Site","location='http://www.snaketray.com'");
mm_menu_0204220223_0.hideOnMouseOut=true;
mm_menu_0204220223_0.bgColor='#003300';
mm_menu_0204220223_0.menuBorder=1;
mm_menu_0204220223_0.menuLiteBgColor='#003300';
mm_menu_0204220223_0.menuBorderBgColor='#003300';

What do I need to add to this script to make www.snaketray.com open in a new window?

Thank you!
 

tlynn721

New Member
Thanks for the quick reply.

I actually did try that first but the external site keep loading in the same page. Very odd. Any other suggestions?
 

conor

New Member
i can't see how that wouldn't work....

if you post the code for the links with the target="_blank" attribute then we might be able to diagnose the problem...
 

DimitriDV

New Member
maybe doctype is too strict? I remember target="_blank" being dropped in the future (not xHtml valid anymore I mean)
 

tlynn721

New Member
Thanks again for the replies.

Dimitri - Here is the doctype code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

Conor - Here is the code that I am pretty sure is relevant to my issue:

mm_menu_0204220223_0.addMenuItem("Snake&nbsp;Tray&nbsp;Main&nbsp;Site", "location='http://www.snaketray.com', target='_blank'");

Perhaps it's something I need to change or include in my mm_menu.js? The script is way too long to put in this reply. I wish I knew more about Javascript and I greatly appreciate all of your help!
 

munkyeetr

New Member
What I use - and which also keeps your page xhtml (strict!) compliant - is the following:

Create a javascript file (in this example, external.js) that contain the following:
Code:
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;

Then link to that in your <head> section with the following:
Code:
<script type="text/javascript" src="external.js"></script>

Now, when you make your links use:
Code:
<a href="http://www.somesite.com" rel="external">LINK TEXT</a>

That should get you working, and xhtml compliant as a bonus :)

As far as using target="_blank" in the traditional sense, I have seen some browsers still open in the same window due (I believe) to options set by the user (or not changed from default by the user) in the browser preferences itself.

Hope this helps,
Munky
 
Top