Jump to content

BB Code JavaScript


Man In Tan

Recommended Posts

I'm trying to put together a bb code inserter for my forum's posting page, but JavaScript confuses me, and I don't seem to be getting anywhere. At the moment, all I'm trying to do is insert the word "test" followed by a two-digit number, into the textarea. Can anyone see what I'm doing wrong?The JavaScript and relevant part of my XHTML are as follows:javascript/bbcode

      ///////  ///////  ///////       //       //       //            ///////  ///////  ///////       //       //       //            //       //       //           /*    FFF: Flat File Forum           © Zero GigaBytes Dot Us              [url="http://0gb.us/"]http://0gb.us/[/url]       */// File: "javascript/bbcodes.js"function fff_simpletag(tag) {old = window.document.postdata.data.value;window.document.postdata.data.write(old + tag);}

<!-- Begining of template: "$php/skins/$skin_xhtml/bbcode_javascript.php" --><script src="javascript/bbcodes.js" type="text/javascript"></script><form name="bbcodes"><select name="bbtag" onchange="fff_simpletag(this.form.bbtag.options[this.form.bbtag.selectedIndex].value)"><option value="test00">test0</option><option value="test01">test1</option><option value="test02">test2</option><option value="test03">test3</option></select></form><!-- End of template: "$php/skins/$skin_xhtml/bbcode_javascript.php" --><form action="post.php" method="post" name="postdata"><table id="posttable"><input type="hidden" name="forum" value="17" /><tr><td class="postleft">Topic Title:</td><td class="postright"><input class="text" type="text" name="title" /></td></tr><input type="hidden" name="post" value="data" /><tr><td class="postleft">Message:</td><td class="postright"><textarea id="post" name="data"></textarea></td></tr><tr><td class="postleft">Submit:</td><td class="postright"><input class="text" type="submit" value="POST" /></td></tr></table></form><!-- End of template: "$php/skins/$skin_xhtml/forum_data.php" -->

Link to comment
Share on other sites

A textarea does not have a write() function. You simply change the value property.FWIW, that code looks pretty old. It's valid, but cumbersome. This in particular is needlessly complex:

<select name="bbtag" onchange="fff_simpletag(this.form.bbtag.options[this.form.bbtag.selectedIndex].value)">

It can be simplified to this:

<select name="bbtag" onchange="fff_simpletag(this.options[this.selectedIndex].value)">

And if you think your users have fairly modern browsers:

<select name="bbtag" onchange="fff_simpletag(this.value)">

Link to comment
Share on other sites

Thank you ever so much! After fixing several typos I made (such as a double function "fff_simpletag(fff_simpletag(this.form.bbtag.options[this.form.bbtag.selectedIndex].value))"), it works great. But, only with the "text/html" content-type header. Does JavaScript not work with the "application/xhtml+xml" content-type?All right, next step.How do you indicate where the courser is? (to insert a single tag in the middle of the textarea)How do you insert tags around selected text? I tried:

<form name="bbcodes"><select name="bbtag" onchange="fff_normaltag(this.value)"><option value="test00">test0</option><option value="test01">test1</option><option value="test02">test2</option><option value="test03">test3</option></select></form>

function fff_nonmaltag(tag) {if(window.getSelection){ var highlightedtext = window.getSelection();window.getSelection() = '[' + tag + ']' + highlightedtext + '[/' + tag + ']'; }else if (document.getSelection){ var highlightedtext = document.getSelection();document.getSelection() = '[' + tag + ']' + highlightedtext + '[/' + tag + ']'; }else if (document.selection){ var highlightedtext = document.selection.createRange().text;document.selection.createRange().text = '[' + tag + ']' + highlightedtext + '[/' + tag + ']'; }}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...