...

View Full Version : Check Selection Script not Running



e39m5
10-15-2006, 05:29 PM
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.


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

Kravvitz
10-15-2006, 08:02 PM
Don't forget that there are many more browsers than IE and Firefox.

Read this: Some signs that a JavaScript Script was Badly Written (http://www.dynamicsitesolutions.com/javascript/badly_written_scripts/)

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.faqts.com/knowledge_base/view.phtml/aid/13562)
http://www.alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript/
http://www.massless.org/mozedit/
http://parentnode.org/javascript/working-with-the-cursor-position/

e39m5
10-15-2006, 09:08 PM
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?


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
10-15-2006, 09:24 PM
I missed a line that I needed. It works now.


strSelection = document.selection.createRange().text;

e39m5

Kravvitz
10-15-2006, 09:39 PM
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:

<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>

e39m5
10-15-2006, 10:34 PM
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:

<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

Kravvitz
10-16-2006, 02:37 AM
It's not working because "new" is a reserved keyword. You need to name the variable something else.

Here's a list of them. (http://javascript.about.com/library/blreserved.htm)

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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum