scriptygoddess

18 Nov, 2002

Link, Bold, Italic links in comment text area

Posted by: Jennifer In: Scripts

Someone requested the code for how to do that "Link", "Bold", and "Italic" shortcut links in the comments section. I first saw it on Dawn's blog sometime ago, and actually, the code in the MT-Entry template is very similar. In either case here it is:

In between your <HEAD> </HEAD> tags, put this:

<script language="javascript">
<!–
function ahrefThis() {
strSelection = document.selection.createRange().text
if (strSelection == "") document.text.text.focus()
strHref = prompt("Enter the URL of the site you to link:","http://")
if (strHref == null) return;
document.selection.createRange().text = "<a href=\"" + strHref + "\" target=\"_blank\" class=\"text\">" + strSelection + "</a>"
return;
}
function boldThis(from) {
strSelection = document.selection.createRange().text
if (strSelection == "") {
document.text.text.focus()
if (from == 2) document.text.text.select()
strSelection = document.selection.createRange().text
document.selection.createRange().text = strSelection + "<b></b>"
}
else document.selection.createRange().text = "<b>" + strSelection + "</b>"
return;
}
function clipThis(from) {
strSelection = document.selection.createRange().text;
document.text.text.focus();
var dummy = document.text.text.value;
document.text.text.value = "";
if (dummy) {
document.selection.createRange().text = dummy + "\n\n<i>" + strSelection + "</i>";
}
else {
document.selection.createRange().text = "<i>" + strSelection + "</i>";
}
return;
}
function italicThis(from) {
strSelection = document.selection.createRange().text
if (strSelection == "") {
document.text.text.focus()
if (from == 2) document.text.text.select()
strSelection = document.selection.createRange().text
document.selection.createRange().text = strSelection + "<i></i>"
}
else document.selection.createRange().text = "<i>" + strSelection + "</i>"
return;
}
//–>
</script>

Then where you want to offer the links for your comments box:

<script language="JavaScript">
<!–
if (document.selection) {
document.write('<span class="signature">highlight your text then click to <A title="Make a Link" href="javascript:ahrefThis();" target=_self>LINK</A>, <A title=Bold href="javascript:boldThis();" target=_self><B>BOLD</B></A> or <A title=Italicize href="javascript:italicThis();" target=_self><I>ITALICIZE</I></A></span><br>')
}
//–>
</script>

Since this code doesn't work with all browsers: I'm hiding it using the javascript "if document.selection". That way those that can't use it, won't see it.

31 Responses to "Link, Bold, Italic links in comment text area"

1 | Babs

November 19th, 2002 at 7:19 pm

Avatar

I hope you don't mind tht I edited the script a little to make it pMcode friendly for my comments in pMachine. There was much tweaking involved. just chaing the < to [ and the > to ]

2 | Babs

November 19th, 2002 at 7:46 pm

Avatar

that should have been there "wasn't" much tweaking.

3 | Jennifer

November 19th, 2002 at 7:54 pm

Avatar

You're always welcome to tweak and hack to your hearts delight, anything you find here… 😀

4 | Gaile

November 21st, 2002 at 4:39 pm

Avatar

Thanks, nice script!

5 | Amy

November 22nd, 2002 at 1:42 pm

Avatar

Don't hit me for saying this…but users should be aware that this function does not work in Mozilla – there's a known bug in createRange() for Mozilla, and it will probably be quite some time before it's fixed. I ran into that while putting together something like this for Quarto, and it drove me crazy until I checked bugzilla.

I'm unsure if it works in Opera, but it merits checking.

6 | Ragnar

December 11th, 2002 at 9:05 am

Avatar

Hi, I'm just wondering if I can use your code for the buttons on the comments template by creating modules with the code and then put MT-Include in the right places in the comments template. Any thoughts on that? :)

7 | Jennifer

December 11th, 2002 at 4:02 pm

Avatar

As long as all the code gets included at some point, I don't see why it wouldn't work.

8 | Michelle

March 27th, 2003 at 6:17 am

Avatar

Woah, that rocks. I'm using it now, too.

9 | Thrinz

March 30th, 2003 at 4:34 am

Avatar

Hello,
I wanted a script which has to refresh only a
piece of text within the page and not the complete piece……………

Could u plz solve my problem……….

10 | randy

May 7th, 2003 at 4:42 pm

Avatar

Hi. This is the script I'm looking for, but into which template do I paste it? The Main Index? The Comment Listing Template? Which script goes where? Also, how do you replace the words "Bold", etc., with the little graphics for Bold, Highlight, and URL? Thanks in advance for any help you can provide – Randy

11 | mnb

May 29th, 2003 at 3:15 am

Avatar

From what I can tell, this script only works on IE on Windows. It doesn't work on IE on the Mac, or Safari, or Netscape on Linux. I'd lay odds it doesn't work anywhere on Netscape or Mozilla.

12 | Maggie

June 1st, 2003 at 12:50 pm

Avatar

In the setup.php it says

In the form tag, search for this line:

action="<$MTCGIPath$>mt-comments.cgi"

And replace it with this:

action="http://divamaggie.com/subscribe/emailtolist.php"

In the Comment Listing Template it acually says

<form method="post" action="<$MTCGIPath$><$MTCommentScript$>" name="comments_form" onsubmit="if (this.bakecookie[0].checked) rememberMe(this)">

When this step is completed what exactly gets changed? Or exactly how should that whole section look when changed? What I'm really getting at is does the <$MTCommentScript$> part of the code remain and if so, exactly how should the section look with the change implemented?

13 | Maggie

June 1st, 2003 at 12:51 pm

Avatar

Also, is there a tutorial somewhere for the following script?

"Please wait… your comment is being submitted."

14 | Maggie

June 1st, 2003 at 12:53 pm

Avatar

Sorry…had two instances open of scriptygoddess. Please disregard my last two comments.

15 | Jefe

August 6th, 2003 at 3:26 am

Avatar

scriptygodess, how I love you so.

16 | Gina

October 25th, 2003 at 12:48 pm

Avatar

This works fine in my blog except that the links aren't getting the target="_blank" added to them and the urls are being opened in the comments window. The bold, italics works fine, and the link gets written, it just doesn't include the target in the output as the script suggests. I was going to add the target myself, then I saw that it should be writing it out.

I'm using IE6, is there a known bug?

17 | Gina

October 25th, 2003 at 12:52 pm

Avatar

Even wierder, is that i just got the comments notification email, and the contents were:

trgtrg tr ttr

However, in the comments pop up and in my expandable comment area, it's written as:

trgtrg tr ttr

What could be causing the target to be stripped out of the comments but emailed as the contents of the comments??????

18 | Jennifer

October 25th, 2003 at 12:55 pm

Avatar

If you're using MT, and you're using the default setting for "comment sanitizing" then MT itself may be stripping them out. Make sure you're using a customized setting and allowing "target" as an attribute to "a" (as well as href, obviously) – and that should fix it.

19 | Jennifer

October 25th, 2003 at 12:57 pm

Avatar

Just an additional note… be careful what you put in the "customized" setting… make sure you include everything you want (and EXCLUDE everything you DON"T want…)

Here's an example of my setting:
(This is all under Weblog Config -> Prefrences -> under Sanitize Spec):

i, a href target, b, br, p, strong, em, ul, li, blockquote, img src width height, h3

20 | Gina

October 25th, 2003 at 1:09 pm

Avatar

Thanks! That did it. I was reading the sanitize specs in the MT manual when I got your last email listing the allowed tags. That helped alot because I wasn't sure what to include and the format with the commas and stuff :) Now it works perfectly! :Kiss:

21 | Susana

January 14th, 2004 at 7:03 am

Avatar

hello!

i've got a web page with text, grids and images. what i need is to automatically highlight the words on my page that are on my DB.
what do i have to change in your code for it to parse my entire page and highlight what i want as soon as the page opens without having to click anything?

22 | ETC. Indulging my inner geek

November 18th, 2002 at 2:11 pm

Avatar

Link, Bold, Italics in Comment Area
Scriptygoddess has come up with another great script – this one for adding bold, italics and links in the blog

23 | Babs - A Day In the Life

November 19th, 2002 at 7:28 pm

Avatar

Click – the pMcode version
Just changed a few things in the script to make it pMcode friendly.

24 | BeaLog

December 19th, 2002 at 9:38 am

Avatar

Shopping & Decoration
This morning, around 11 a.m., the hubby told me to get ready because he had a surprise for me. As

25 | Happy Pills

April 6th, 2003 at 1:58 pm

Avatar

pretty spiffy
Check it out — I just spiffied up the comment form a bit :-) I…

26 | Bloggie Broad

September 14th, 2003 at 7:01 pm

Avatar

Bloggie's Scripty Scripts
Someone asked me which scripts I used to make all the cool things magically appear here on BB. I found nearly everything at Scriptygoddess –…

27 | Bloggie Broad

October 8th, 2003 at 12:42 am

Avatar

Reverb: Bloggie Scripty Fun
I'm organizing BB and deleted the posts revolving around my techie mess from earlier in the week, since it was an easy fix. I like…

28 | Bloggie Broad

November 23rd, 2003 at 5:48 pm

Avatar

Reverb: Bloggie Scripty Fun
I'm organizing BB and deleted the posts revolving around my techie mess from earlier in the week, since it was an easy fix. I like…

29 | LilacRose

November 28th, 2003 at 5:14 pm

Avatar

Christmas Skin, etc.
I'm not quite "back" from my break yet, but I did want to go ahead and tell you about the…

30 | MadMan

August 1st, 2004 at 5:21 am

Avatar

Hiya. Do you know of any hacks to make this work in Firefox/Mozilla? :)

31 | Jennifer

August 1st, 2004 at 6:50 am

Avatar

It's not exactly the same script – but it's what I'm using on this site, and it does work in firefox/mozilla: See post here

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements