Help - Search - Members - Calendar
Full Version: How do I add target="_blank" to my link button?
Movable Type Community Forum > Additional Resources > Tips and Tricks
fanzing
This is a question about if it is possible to modify the coding of the buttons in the "add post" interface.

There are the handy buttons for adding b, i and URL above the posting field.  I'd like to modify the URL button's coding slightly.

Right now, when you highlight text and click the URL button, then paste in your URL, you get this:
CODE
<a href="LINK">TEXT</a>


What I want to do is add a blank target, like so:
CODE
<a href="LINK" target="_blank">TEXT</a>


Most everyone should recognize this.  Clicking on that link would then open the link in a new window instead of sending them away from my site.

My reason for wanting to change the button coding are:

1) It's a time-saver for me, and if it's up to me to do it manually, I'll grow sick of it or forget sometimes.
2) Even if I remember to manually code it every time, not all of the other authors for my site will remember.

Is there any way to change this?
medic119
Its been mentioned here a bunch of times, just do a search on traget=_blank and it should pop-up.

At any rate you have to hack the edit_entry.tmpl file in your mt installation.
L7 Productions
I've just gotten so used to adding the title, class, target, and href in manually that I haven't even thought about MT doing it automatically.  ;)
medic119
OK, here is an excerpt of an article I just wrote as this particular feature is one of the most asked I see on this board.  It should explain how to add those attributes:

************
It involves editing the edit_entry.tmpl and the bm_entry.tmpl, however. The edit_entry.tmpl is for the Edit/Add entry screen and the bm_entry.tmpl is for the bookmarklet.

Open them up in your favorite text editor and search for the following string: function insertLink (). That will find a segment of code that looks like this:

CODE
function insertLink () {
   if (!document.selection) return;
   var str = document.selection.createRange().text;
   if (!str) return;
   var my_link = prompt('<MT_TRANS phrase="Enter URL:">', 'http://');
   if (my_link != null)
       document.selection.createRange().text = '<a href="' + my_link + '" >'
       + str + '</a>';
}


Find the line:

CODE
document.selection.createRange().text = '<a href="' + my_link + '" >' + str + '</a>';


Here you will add your target="_blank" attribute. The line should then look like:
CODE
document.selection.createRange().text = '<a href="' + my_link + '" target="_blank" >'
+ str + '</a>';


Once you have completed these steps, reopen your FTP client and upload these files back to your server in ASCII Mode.

WARNING: Make a backup of the original edit_entry.tmpl and bm_entry.tmpl before starting this edit.
fanzing
Thanks, doc, the code works wonderfully...but I have one more question.

This solution helps add the _blank to the URL when I use the URL button on "add post".

But what about if I use a bookmarklet to post a page I'm visiting? When I select MT IT!, the pop-up appears with the URL link already made...but it doesn't have the _blank in it. Is there a way to add that as well? I'm looking through this coding and it's just not jumping out at me.
mversion
Hmm... this gives an idea. Considering how it is morally incorrect(!) nowadays to 'force' links to open in a new window, instead of hacking MT to include target="_blank" how would one go about hacking MT to include the link and an icon next to it with the target="_blank" attribute?

I do this on my blog but it is partially manual. Heck, I have done it for years now. I use a bookmarklet that automatically inserts the image and attribute after asking for input.

But I am sure if someone here really put their head to it they could have edit_entry.tmpl hacked to produce a code such as
CODE
<a href="LINK.COM">LINK TEXT</a> <a href="LINK.COM" target="_blank"><img src="ICON.gif" />


So the user can click on the text link to open the link in the same window, or click on the icon to open the link in a new window.

Hope I didn't complicate matters.
asdren
Now does editing edit_entry.tmpl only affect the posts made in the comments?

I want the links that I put in my entries to automatically add the target="blank" and this didn't seem to affect those. Is the re a setting in the admin that I overlooked?

BTW, I put my first blog up less 24hrs ago. Excellent software. If MT was created by two people then I am extremely impressed.
girlie
This example of editing edit_entry.tmpl doesn't affect the comments at all - it's meant to enhance the output of the little URL button on the Edit Entry screen function by adding target="_blank" to every link you create in your entry by using that button.

So it sounds like it does do what you want?

(Yep, two people: Ben and Mena Trott. Impressive indeed. smile.gif )
Pauley
I wonder if this might be what you're looking for:

You can add a BASE tag to the HEAD section of of your HTML. Just before you close the HEAD section, put in the tag:

CODE
<base target="_blank">


ALL of your links, unless otherwise specified, will new pop up in a new window. This makes it easier to make sure that all the links pop up in a new window; you now only need to make sure that all the links you DO want in the same window have target="_top" specified.

This is how I have *awkward pause* set up -- you'll find the BASE tag just prior to the end of the HEAD section.

-- Pauley
TVerBeek
QUOTE (asdren @ Jun 21 2003, 07:42 AM)
I want the links that I put in my entries to automatically add the target="blank" and this didn't seem to affect those.

I installed the MTMacro plug-in, and (among other thigns) included the following in my macro "library":
CODE
<MTMacroDefine name="targetblank" string="a href">a target="_blank" href</MTMacroDefine>

Since there's no need to put the HREF= before the TARGET= in a link tag, it works like a charm, inserting that snippet into every link in my entries, but leaving the rest of the links in the site alone.
fliptophead
I've been having a problem with MTMacro. Here's the error I'm getting:

CODE
An error occurred:

Can't locate bradchoate/macros.pm in @INC (@INC contains: /usr/lib/perl5/5.6.1/i386-linux /usr/lib/perl5/5.6.1 /usr/lib/perl5/site_perl/5.6.1/i386-linux /usr/lib/perl5/site_perl/5.6.1 /usr/lib/perl5/site_perl/5.6.0/i386-linux /usr/lib/perl5/site_perl/5.6.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.6.1/i386-linux /usr/lib/perl5/vendor_perl/5.6.1 /usr/lib/perl5/vendor_perl . /usr/local/apache1.3.26-2/ /usr/local/apache1.3.26-2/lib/perl) at plugins/macros.pl line 41.


Can anyone see with this error what I'm doing wrong? I can't quite figure it out.
kadyellebee
Has the Macros plugin worked for you in the past, or is this the first time you are trying to use it?

Double check where you uploaded the macros.pm file to; it looks like its not finding it in the correct place.

Kristine
fliptophead
No, it hasn't yet worked for me. I've gotten this error with other Brad Choate plug-ins as well, though. Not sure what I'm doing wrong. I've read over the documentation and seem to have everything where it needs to be. I'm still not sure what I'm doing wrong.

Sorry for the double post on this question. I posted it earlier and didn't realize it wasn't exactly appropriate for the topic that was already going. Tried to delete it but couldn't.

huh.gif
inscrutably
where is this file located? edit_entry.tmpl
shelley
On your server in the folder where you installed MT, in tmpl/cms/.
inscrutably
So I opened up the edit_entry and change the section insert link but now when I go to my mt.cgi this comes up and I can't go past that screen

CODE
MT::App::CMS=HASH(0x83a810c) Use of uninitialized value in split at lib/MT/App.pm line 118.


I uploaded them once more from the movabletype.org download and it worked but when I click on save either changes to templates or added entries it goes back to the mt.cgi entry page where I need to type my password.
shelley
That error means its having trouble setting the cookie - try deleting your cookies before attempting another login.
inscrutably
thanks it worked! smile.gif
s_esquibel
It should probably be noted that in MT 3.15 (I really don't know the actual version where this starts, maybe 3.0D or something) the fix shown above does not work.

The change needs to be made in mt.js around line 163.

Before:
CODE
function insertLink (e, isMail) {
   if (!canFormat) return;
   var str = getSelection(e);
   if (!str) return;
   var my_link = isMail ? prompt('Enter email address:') : prompt('Enter URL:', 'http://');
   if (isMail) my_link = 'mailto:' + my_link;
   if (my_link != null)
       setSelection(e, '<a href="' + my_link + '">' + str + '</a>');
   return false;
}


After Change:
CODE
function insertLink (e, isMail) {
   if (!canFormat) return;
   var str = getSelection(e);
   if (!str) return;
   var my_link = isMail ? prompt('Enter email address:') : prompt('Enter URL:', 'http://');
   if (isMail) my_link = 'mailto:' + my_link;
   if (my_link != null)
       setSelection(e, '<a href="' + my_link + '" target="_blank">' + str + '</a>');
   return false;
}


It took me a couple of greps to find this.
Robmonster
QUOTE (s_esquibel @ Feb 12 2005, 10:03 AM)
It should probably be noted that in MT 3.15 (I really don't know the actual version where this starts, maybe 3.0D or something) the fix shown above does not work.

The change needs to be made in mt.js around line 163.

<snip>

This doesnt seem to work... I've made the change to the mt.js file as described, uploaded it, rebuilt the site and the target=blank isnt getting added.

Any more ideas?

RM
s_esquibel
QUOTE (Robmonster @ Feb 15 2005, 03:38 PM)
This doesnt seem to work... I've made the change to the mt.js file as described, uploaded it, rebuilt the site and the target=blank isnt getting added.

Any more ideas?

RM

This change only affects when you click on the URL link on top of the entry form. Rebuilding does not replace all of your previous links as mt.js is only used in the MT user screens.

You may want to check into a meta tag or something that forces all links to be opened in a new window. See the comment in this thread by Pauly.

QUOTE
I wonder if this might be what you're looking for:

You can add a BASE tag to the HEAD section of of your HTML.  Just before you close the HEAD section, put in the tag:

CODE
<base target="_blank">


ALL of your links, unless otherwise specified, will new pop up in a new window.  This makes it easier to make sure that all the links pop up in a new window; you now only need to make sure that all the links you DO want in the same window have target="_top" specified.

This is how I have *awkward pause* set up -- you'll find the BASE tag just prior to the end of the HEAD section.

-- Pauley
Robmonster
How wierd....

I understand what you mean in that rebuilding wont do anything.

At the moment I am composing a new entry.

I highlight the work I want to be a URL link, click the Insert Link button. A window pops up titled Enter URL. I enter the URL and it wraps it around the text as it should, just without the target=_blank section. I've double checked and I have definately uploaded the new, edited mt.js file. Perhaps I need to clear my browser cache in case its reading in the old one....

I take it if I went the Meta tag route then ALL links on ym blog would open in new windows, even though merely pointing to other blog entries....?

RM
Robmonster
Clearing the browser cache seemed to do the trick. Its now putting the full tag in for me. C'est magnifique!!

RM
ton007
Just a thought: would it be possible to add an insert target="_blank" here button in the current button line-up above the entry fields?
Robmonster
You probably could if you knew javascript.
sk4sk4
QUOTE (medic119 @ Mar 25 2003, 07:28 PM)
OK, here is an excerpt of an article I just wrote as this particular feature is one of the most asked I see on this board. �It should explain how to add those attributes:

************
It involves editing the edit_entry.tmpl and the bm_entry.tmpl, however. The edit_entry.tmpl is for the Edit/Add entry screen and the bm_entry.tmpl is for the bookmarklet.

Open them up in your favorite text editor and search for the following string: function insertLink (). That will find a segment of code that looks like this:

CODE
function insertLink () {
� �if (!document.selection) return;
� �var str = document.selection.createRange().text;
� �if (!str) return;
� �var my_link = prompt('<MT_TRANS phrase="Enter URL:">', 'http://');
� �if (my_link != null)
� � � �document.selection.createRange().text = '<a href="' + my_link + '" >'
� � � �+ str + '</a>';
}


Find the line:

CODE
document.selection.createRange().text = '<a href="' + my_link + '" >' + str + '</a>';


Here you will add your target="_blank" attribute. The line should then look like:
CODE
document.selection.createRange().text = '<a href="' + my_link + '" target="_blank" >'
+ str + '</a>';


Once you have completed these steps, reopen your FTP client and upload these files back to your server in ASCII Mode.

WARNING: Make a backup of the original edit_entry.tmpl and bm_entry.tmpl before starting this edit.
*


i'm running 3.2 and can't find this code in the edit_entry.tmpl file. is it somewhere else?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.