PDA

View Full Version : bbcode insert only in IE?



insanemonkey
01-15-2008, 08:43 PM
hello everyone, I have found a script where it inserts bbcode into my textarea form, the only thing is wron, it doesnt work in Firefox only IE, it refreshes the page when you click the button.

This is the js script-

<SCRIPT LANGUAGE=javascript>
<!--
var undoLastText = "Reset (no previous record)"

function storeCaret(TextAreaContent)
{
if (TextAreaContent.createTextRange) TextAreaContent.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret(TextAreaContent, text)
{
if (TextAreaContent.createTextRange && TextAreaContent.caretPos)
{
var caretPos = TextAreaContent.caretPos;
undoLastText = document.all.reply.yourpost.value;
caretPos.text = '['+text+']'+caretPos.text+'[/'+text+']';
document.all.FinishedText.innerHTML = replace(document.all.reply.yourpost.value,'\n','<BR>');

}
else
alert("Please select some text for formatting!");
}
function insertLinkAtCaret(TextAreaContent, text)
{
if (TextAreaContent.createTextRange && TextAreaContent.caretPos)
{
var caretPos = TextAreaContent.caretPos;
undoLastText = document.all.reply.yourpost.value;
cheese = prompt('Please enter the full URL for this link','http://');
caretPos.text = '['+text+'="'+cheese+'"]'+caretPos.text+'[/'+text+']';
document.all.FinishedText.innerHTML = replace(document.all.reply.yourpost.value,'\n','<BR>');
}
else
alert("Please select the word or phrase which you wish to hyperlink");
}
function replace(string,text,by) {
// Replaces text with by in string
var strLength = string.length, txtLength = text.length;
if ((strLength == 0) || (txtLength == 0)) return string;

var i = string.indexOf(text);
if ((!i) && (text != string.substring(0,txtLength))) return string;
if (i == -1) return string;

var newstr = string.substring(0,i) + by;

if (i+txtLength < strLength)
newstr += replace(string.substring(i+txtLength,strLength),text,by);

return newstr;
}
function undoLast()
{
document.all.reply.yourpost.value = undoLastText;
document.all.FinishedText.innerHTML = replace(document.all.reply.yourpost.value,'\n','<BR>');

}
function addText(text){
document.getElementById('yourpost').value += text;
}


//-->
</SCRIPT>


and this is what i use to insert a button command into the textarea.


<Button TYPE="image" VALUE="B" onClick="insertAtCaret(this.form.yourpost,\'B\');" ID="BoldButton" NAME="BoldButton" style="BORDER-RIGHT: #ffffff 0px; BORDER-TOP: #ffffff 0px; BORDER-LEFT: #ffffff 0px; BORDER-BOTTOM: #ffffff 0px; BACKGROUND-COLOR: #ffffff;">
<img src="http://www.xudas.com/forum/forum/images/bold.gif"></button>

can anyone tell me why it only works in IE and not firefox or opera, also i dont want to use one of those html editor things I like my own simple one, so if you found any tutorials please help.

pman
01-16-2008, 01:38 AM
Can you post a link to that page.

Also, as I recommend everyone, try using Firebug plugin for Firefox when debugging javascript. It will save you a lot of time, show you where the problem is.

onClick="insertAtCaret(this.form.yourpost,\'B\');"

the insertAtCaret(..) function you have

TextAreaContent.createTextRange and TextAreaContent.caretPos

I believe createTextRange and caretPos are IE specific functions for textarea. That's why it works fine for IE but not in Firefox or Opera.

pman
01-16-2008, 01:54 AM
Unfortunately you will have to use createTextRange and such for IE. There's no single function that works on all browsers(as far as I know). For Mozilla, Firefox, (don't know about Opera) use createRange

Here, this should help

http://developer.mozilla.org/en/docs/DOM:range

if(document.selection)
{
... for IE
}
else if(window.selection)
{
... for non IE
}

insanemonkey
01-17-2008, 03:21 AM
thansk for your guys's links. oncei get back home i will try it out and see.. thanks post #3 thats what i kinda want in javascript, I really dont know much about javascript but is there a script or some kind of tutorial on a excellent bbcode insert script, im not to good at javascript and dont really have time to learn it at the moment, thanks.

the link to the script is on my site on the forums when you make a reply and want to insert a smilie, it makes the page refresh in firefox and opera but not IE