Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 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
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I missed a line that I needed. It works now.

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

  • #5
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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; 10-15-2006 at 09: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.

  • #6
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •