Forum Moderators: open

Message Too Old, No Replies

Standards Compliant?

         

rmarcks

4:57 pm on May 25, 2007 (gmt 0)

10+ Year Member



I found the following script (albeit, 3 yrs old), at SitePoint. (http://www.sitepoint.com/article/standards-compliant-world). The intent of the script is to use XHTML in a standards compliant way to open a new window using javascript. This is in lieu of the deprecated target attribute. My problem is the script seems to work only in Opera. Under IE and Gecko-based browsers, the script does not generate an error, it simply doesn't work.

**********************************

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;

**********************************

I used the above as inspiration to clean-up inline event-handlers in a tooltip script I've been using for years. The script is listed below and includes a couple of 'troubleshooting' lines that indicate the script is running through. I then took a page with my original tooltip script so that I know it works in IE/Opera/Gecko. I modified the page using both of the above scripts. It has the same problem; works in Opera only while causing no errors in IE or Gecko-derivatives.

**********************************

function tips()
{
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
var j=0;
for (var i=0; i<anchors.length; i++)
{
var tips = anchors[i];
if (tips.className == "tip")
{
j++;
var tipwin = "tipWin" + String(j);
tips.onmouseout = "tipDown('"+tipwin+"')";
tips.onmouseover = "toolTips(event,'"+tipwin+"',10);return true";
document.getElementsByTagName("a")[j].onclick = "alert('Hello')"; //troubleshooter
}
}
document.getElementById('test').value=tipwin; //troubleshooter
}
window.onload = tips;

**********************************
Oddly enough, if one installs these scripts and use the Gecko DOM inspector to see what's happening, the eventhandlers will appear where they are suppose to, but apparently do not fire.

Any comments/help?

RKM

Fotiman

5:34 pm on May 25, 2007 (gmt 0)

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




window.onload = tips;

Is it possible that you have additional code that's overwriting your window.onload? For example:

window.onload = externalLinks;
window.onload = tips;

or

window.onload = tips;
...
<body onload="...">

rmarcks

7:17 pm on May 26, 2007 (gmt 0)

10+ Year Member



It turns out I didn't have another onload function, but it was something similar...variables overwriting each other. That coupled with differences in browsers made this one time consuming to fix. Here is my final solution w/o the scripts being called:

****************************

function externalLinks()
{
if (!document.getElementsByTagName){return;}

var anchors = document.getElementsByTagName("a");
var j=0;
for (var i=0; i<anchors.length; i++)
{
var anchor = anchors[i];

if (anchor.className == "tip")
{
j++;
var tipWin = "tipWin" + String(j);
var tip = "tip" + String(j);

if (navigator.userAgent.indexOf('MSIE')==-1)
{
eval("function show(event){toolTips.call(document.getElementById('"+tip+"'),event,'"+tipWin+"')}");
eval("function hide(event){tipDown.call(document.getElementById('"+tip+"'),'"+tipWin+"')}");
}
else
{
eval("function show(){toolTips.call(document.getElementById('"+tip+"'),event,'"+tipWin+"')}");
eval("function hide(){tipDown.call(document.getElementById('"+tip+"'),'"+tipWin+"')}");
}

anchor.onmouseout = hide;
anchor.onmouseover = show;
}

if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
{anchor.target = "_blank";}
}
}
window.onload = externalLinks;
************************************

Thanks for pointing me in the right direction

RKM