Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-15-2006, 05:29 PM   PM User | #1
e39m5
New to the CF scene

 
Join Date: Oct 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
e39m5 is an unknown quantity at this point
Check Selection Script not Running

This is my first time writing in JS and I am having some trouble. This code was put together with a bunch of different codes. It is going to be used as part of a larger code when it is finished.

Right now, this code does nothing, I click the button and no alerts come up.

Code:
script language="JavaScript" type="text/javascript">
<!--
function checkSelection() {
	//IE
	var field = document.post.testfield;
	if (document.all) {
		//Text is Selected
		if (strSelection!="") {
			alert ("IE: Text is Selected")
		}
		//Text is not Selected
		else {
			alert ("IE: Text is not Selected")
		}
	}
	//FF
	else if (document.getElementById) {
		//Text is Selected
		if(field.selectionStart - field.selectionEnd != 0){
			alert ("Firefox: Text is Selected")
		}
		//Text is not Selected
		else {
			alert ("Firefox: Text is not Selected")
		}
	}
}
//-->
</script>
<form action="posting.php" method="post" name="post">
<textarea name="testfield">
Some sample teext for testing.
</textarea>
<input type="button" value="Test" onclick="checkSelection();">
</form>
Any help is appreciated.

Thanks,
e39m5
e39m5 is offline   Reply With Quote
Old 10-15-2006, 08:02 PM   PM User | #2
Kravvitz
Senior Coder

 
Join Date: Feb 2006
Location: USA
Posts: 1,013
Thanks: 0
Thanked 0 Times in 0 Posts
Kravvitz is an unknown quantity at this point
Don't forget that there are many more browsers than IE and Firefox.

Read this: Some signs that a JavaScript Script was Badly Written

These should help you:
How can I manipulate the selection and the caret in an input type="text" element in Mozilla browsers and IE/Win?
http://www.alexking.org/blog/2003/06...ng-javascript/
http://www.massless.org/mozedit/
http://parentnode.org/javascript/wor...rsor-position/
__________________
Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
Java != JavaScript && JScript != JavaScript
Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.
Kravvitz is offline   Reply With Quote
Old 10-15-2006, 09:08 PM   PM User | #3
e39m5
New to the CF scene

 
Join Date: Oct 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
e39m5 is an unknown quantity at this point
I have seen those scripts before and mozedit was a major resource in developing the piece of code I already made, except it is not working.

You can see the similarities between these three functions and my code. I have something different to do with the text if it is not selected, so I attempted to make an if statement to check if it is selected or not. Except it doesn't work, thats why I posted this. Do you see any problems in my code?

Code:
	function mozWrap(txtarea, lft, rgt) {
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		if (selEnd==1 || selEnd==2) selEnd=selLength;
		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + lft + s2 + rgt + s3;
	}
	
	function IEWrap(lft, rgt) {
		strSelection = document.selection.createRange().text;
		if (strSelection!="") {
		document.selection.createRange().text = lft + strSelection + rgt;
		}
	}
	
	function wrapSelection(txtarea, lft, rgt) {
		if (document.all) {IEWrap(lft, rgt);}
		else if (document.getElementById) {mozWrap(txtarea, lft, rgt);}
	}
e39m5
e39m5 is offline   Reply With Quote
Old 10-15-2006, 09:24 PM   PM User | #4
e39m5
New to the CF scene

 
Join Date: Oct 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
e39m5 is an unknown quantity at this point
I missed a line that I needed. It works now.

Code:
strSelection = document.selection.createRange().text;
e39m5
e39m5 is offline   Reply With Quote
Old 10-15-2006, 09:39 PM   PM User | #5
Kravvitz
Senior Coder

 
Join Date: Feb 2006
Location: USA
Posts: 1,013
Thanks: 0
Thanked 0 Times in 0 Posts
Kravvitz is an unknown quantity at this point
Edit: I started writing this before you figured out why it didn't work in IE/Win.

In your script you didn't set "strSelection", so no, it won't work in IE/Win.

It works in Firefox though. It won't work in Opera because opera supports document.all but not IE's method of doing this. Opera 8+ supports Firefox's (Mozilla's) way of doing this.

Just because one of the scripts uses object detection in an idiotic way doesn't mean you should copy it.

It should look more like this:
Code:
<script type="text/javascript"><!--
function checkSelection() {
  //IE/Win
  var field = document.post.testfield;
  if (document.selection && document.selection.createRange) {
    //Text is Selected
    strSelection = document.selection.createRange().text;
    if (strSelection!="") {
      alert ("IE: Text is Selected");
    }
    //Text is not Selected
    else {
      alert ("IE: Text is not Selected");
    }
  }
  //FF and Opera 8+
  else if (field.selectionStart || field.selectionStart == '0') {
    //Text is Selected
    if(field.selectionStart - field.selectionEnd != 0){
      alert ("Firefox: Text is Selected");
    }
    //Text is not Selected
    else {
      alert ("Firefox: Text is not Selected");
    }
  }
}
// -->
</script>
__________________
Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
Java != JavaScript && JScript != JavaScript
Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

Last edited by Kravvitz; 10-15-2006 at 09:46 PM..
Kravvitz is offline   Reply With Quote
Old 10-15-2006, 10:34 PM   PM User | #6
e39m5
New to the CF scene

 
Join Date: Oct 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
e39m5 is an unknown quantity at this point
I thought something might be messed up in my browser detection, but then I had it working in FF and IE so I figured it was OK. Thanks for that info. Is all that you changed the 2 if statements?

I just added in the next big chunk of code, and it is not working again. Are all of my firefox functions going to work in Opera too?

My final result is going to be a bbCode editor. I realize there are some out there already, but none that work exactly how I need them to and I would like to learn some javascript. Thanks for all you're help so far, I thought I would be able to get past this step without any help, but no such luck.

Here is my new code:
Code:
<script language="JavaScript" type="text/javascript">
<!--
function addBB(type) {
	//IE and Win
	var field = document.post.testfield;
	if (document.selection && document.selection.createRange) {
		//Text is Selected
		strSelection = document.selection.createRange().text;
		if (strSelection!="") {
			if (type == b || type == i || type == u){
				var lft = '[' + type + ']';
				var rgt = '[/' + type + ']';
				IEWrap(lft, rgt);
			}
		}
		//Text is not Selected
		else {
			if (type == b || type == i || type == u){
				var txt = prompt('Enter the text you would like the styple applied to', '');
				var new = '[' + type + ']' + txt + '[/' + type + ']';
				IECursor(field, new);
			}
		}
	}
	//FF and Opera
	else if (field.selectionStart || field.selectionStart == '0') {
		//Text is Selected
		if(field.selectionStart - field.selectionEnd != 0){
			if (type == b || type == i || type == u){
				var lft = '[' + type + ']';
				var rgt = '[/' + type + ']';
				MozWrap(field, lft, rgt);
			}
		}
		//Text is not Selected
		else {
			if (type == b || type == i || type == u){
				var txt = prompt('Enter the text you would like the styple applied to', '');
				var new = '[' + type + ']' + txt + '[/' + type + ']';
				MozCursor(field, new);
			}
		}
	}
}

	function IECursor(field, value) {
		field.focus();
		sel = document.selection.createRange();
		sel.text = value;
	}
	function IEWrap(lft, rgt) {
		document.selection.createRange().text = lft + strSelection + rgt;
	}
	function MozCursor(myField, myValue) {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);
	}
	function MozWrap(txtarea, lft, rgt) {
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + lft + s2 + rgt + s3;
	}
//-->
</script>
<form action="posting.php" method="post" name="post">
<textarea name="testfield">
Some sample teext for testing.
</textarea>
<img src="asdf.jpg" onclick="addBB('b');">
</form>
Thanks again,
e39m5
e39m5 is offline   Reply With Quote
Old 10-16-2006, 02:37 AM   PM User | #7
Kravvitz
Senior Coder

 
Join Date: Feb 2006
Location: USA
Posts: 1,013
Thanks: 0
Thanked 0 Times in 0 Posts
Kravvitz is an unknown quantity at this point
It's not working because "new" is a reserved keyword. You need to name the variable something else.

Here's a list of them.

I recommend you use Firefox's JavaScript/Error Console -- it's what told me what your error was.

Why did you put the defining statements for "lft" and "rgt" inside the conditionals?
__________________
Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
Java != JavaScript && JScript != JavaScript
Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.
Kravvitz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 +1. The time now is 09:37 PM.


Advertisement
Log in to turn off these ads.