Page 1 of 1

Adding target attribute to links. for those that want to

PostPosted: Sat Aug 21, 2010 12:58 pm
by BillyRayPreachersSon
For those that are wanting to add an option to open links in a new window, and don't need to worry about validating against XHTML strict, here's how I achieved it. The end result is a new checkbox in the link edit dialog which lets you toggle whether the link opens in a new window or not.

These changes assume WYMeditor v0.5 RC 2, and also assume you'll be editing the uncompressed version of "jquery.wymeditor.js".

1) Add a new constant anywhere in the constants block at the top of the file:
Code: Select all
TARGET : "target"

2) Add a new selector anywhere in the selectors block:
Code: Select all
targetSelector : ".wym_target"

3) Update the "dialogLinkHtml" markup (near line 564) to insert the following markup before the submit button DIV container:
Code: Select all
+ "<div class='row'>"
+ "<label>Open in new window?</label>"
+ "<input type='checkbox' class='wym_target' value=' target=\"_blank\"' />"
+ "</div>"

4) Update the "WYMeditor.INIT_DIALOG" function and insert the following markup at the very end inside the "if(selected)" block to do with auto-populating fields:
Code: Select all
if (jQuery(selected).attr(WYMeditor.TARGET) == '_blank') {
    jQuery(wym._options.targetSelector).each(function() {
        this.checked = true;

5) In the same "WYMeditor.INIT_DIALOG" function a bit further down, inside the click event handler for "wym._options.dialogLinkSelector", right after the "link.HREF" and "link.TITLE" attributes are being set, add this:
Code: Select all
var newWinCheckbox = jQuery(wym._options.targetSelector)[0];
if (newWinCheckbox.checked) {
    link.attr(WYMeditor.TARGET, '_blank');
} else {

6) Last but not least, update the "WYMeditor.XhtmlValidator" structure for "_tags" -> "a" -> "attributes", and add a sixth item after the "type" attribute:
Code: Select all

This has been working a treat for me on several sites.

Of course, you should take a backup of the file before editing it in case something goes wrong!