Web Analytics Made Easy -
StatCounter Check Selection Script not Running - CodingForum

Announcement

Collapse
No announcement yet.

Check Selection Script not Running

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #2
    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.

    Comment


    • #3
      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 = [B]txtarea.selectionStart[/B];
      		var selEnd = [B]txtarea.selectionEnd[/B];
      		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;
      		[B]if (strSelection!="") {[/B]
      		document.selection.createRange().text = lft + strSelection + rgt;
      		}
      	}
      	
      	function wrapSelection(txtarea, lft, rgt) {
      		[B]if (document.all) {[/B]IEWrap(lft, rgt);}
      		[B]else if (document.getElementById) {[/B]mozWrap(txtarea, lft, rgt);}
      	}
      e39m5

      Comment


      • #4
        I missed a line that I needed. It works now.

        Code:
        strSelection = document.selection.createRange().text;
        e39m5

        Comment


        • #5
          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>
          Last edited by Kravvitz; Oct 15, 2006, 05:46 PM.
          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.

          Comment


          • #6
            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

            Comment


            • #7
              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.

              Comment

              Working...
              X