The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Standards Compliant?

I found the following script (albeit, 3 yrs old), on this site (http://www.sitepoint.com/article/sta...mpliant-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.

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.

**********************************
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.

target is just as deprecated if added from JavaScript as it is if you hard code it. You should not be using JavaScript to add deprecated tags to a page with a strict doctype, if you want to use deprecated tags use a transitional doctype. If you want a strict doctype and to open links in a new window then use the JavaScript window.open() command which is the right way of performing this behaviour.

I know all of that, but it's not my question. I tend to agree with what you said, and I have used onclick/onkeypress=window.open() for quite a number of years now; and probably more frequently than I should. But my question remains unanswered, why does the above script not work?

I did rework the script and got the .target part of it to work by combining the two into a single script as follows.

******************************************
I would still like to know how to dynamically assign/change an an event handler.
I still cannot get any browser other than Opera to recognize and perform the first if statement properly, despite the fact I've shown the assignment to the event handler to exist...at least from my point of view

I was kind of headed in that direction of thought, but was distracted by the fact that Opera evaluated the string as a function. For some reason, I thought the other browsers should do the same. Silly me!

I've applied the eval() and call() functions and all now works as it should.

Just remember that as you are setting a target that you must use a transitional doctype as strict doctypes do not allow you to use target (and usuing JavaScript to add the invalid attribute so that the validator doesn't see it doesn't mean that the page is valid, it just means that you are not validating the final version of the page).

IIRC, the target attribute works in more situations than window.open() though or is at least a whole lot simpler than completely replicating it's behavior with window.open() and other JavaScript/DOM code. Unfortunately I don't remember the specifics from when I was working on this last year.

FYI, we don't use quite that technique on sitepoint.com anymore - I replaced it with this more generic approach:

Code JavaScript:

/* NEW POPUP INITIALISATION CODE based on document click handler to bypass load dependency
This code uses an unencapsulated document.onclick handler, which is necessary
because it's the only way to suppress the default action in bloody Safari */
document.onclick=function(e){//ignore button presses that aren't left button in Firefoxif(typeof e !='undefined'&&typeof e.which!='undefined'&& e.which!=1){returntrue;}//get event node referencevar node = e ? e.target: window.event.srcElement;//iterate upwards from event node until we find a link//but abandon iteration and return true on the link//if we run out of nodes (because we're already at the body) or reach the bodywhile(node.nodeName.toLowerCase()!='a'){if(!node.parentNode){returntrue;}elseif(node.nodeName.toLowerCase()=='body'){returntrue;}else{ node = node.parentNode;}}//if we got this far we have a link reference//so look for a rel attribute if(node.getAttribute('rel')){//there's no such thing as an XHTML compliant target attribute//but ho hum, better preserve the functionality anyway [for now ;)]if(node.rel=='external'){
node.target='_blank';returntrue;}}//if we got this far we don't have rel information//so just follow the link as normalreturntrue;};

Which bypasses the dependency on window.onload, and removes the need for an iterative process at page load.