Go Back   Dynamic Drive Forums > General Coding > JavaScript
Search Dynamic Drive Forums:

Reply
 
Thread Tools Search this Thread
  #1  
Old 01-15-2008, 07:43 PM
insanemonkey insanemonkey is offline
Regular Coders
 
Join Date: Oct 2005
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
Default bbcode insert only in IE?

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-
Code:
<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.
Code:
<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.
__________________
Hey new design new look, goto xudas for personal webdsign help.. (:
Reply With Quote
  #2  
Old 01-16-2008, 12:38 AM
pman pman is offline
Junior Coders
 
Join Date: May 2007
Location: Canada
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Last edited by pman; 01-16-2008 at 12:44 AM. Reason: update
Reply With Quote
  #3  
Old 01-16-2008, 12:54 AM
pman pman is offline
Junior Coders
 
Join Date: May 2007
Location: Canada
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
}
Reply With Quote
  #4  
Old 01-17-2008, 02:21 AM
insanemonkey insanemonkey is offline
Regular Coders
 
Join Date: Oct 2005
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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
__________________
Hey new design new look, goto xudas for personal webdsign help.. (:
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:46 AM.

Home - Contact Us - Archives - Link to DD - Top 

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.