473,549 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

global var doesn't capture text on mouseup



I've got a function, you can see it below, that is being called
onmouseup in the textarea on my main form. The idea is to find a
selection if possible and store that text in a global variable. I can't
get this to work in any browser on a Mac, though it works alright on a
PC. What am I missing?

// 01-24-05 - most of the time, with most browsers, you
can't get a selection from a form
// input with a function triggered with a form control
like a button, because clicking the
// button shifts the focus to the button and destroys
the selection. So I'm thinking that
// perhaps we can have a global variable that is set
with setFieldSelecti onValue() which
// can be called onMouseUp(). This sets the value and
insertAtCursor( ) can check to see
// if that value is there.
var fieldValue = "";

function setFieldSelecti onValue(myField ) {
// 01-24-05 - this next line is meant to ensure
that we don't
// carry an old value forward.
fieldValue = "";

if (document.selec tion) {
if (document.selec tion.createRang e) {
var range =
document.select ion.createRange ();
var fieldText = range.text;
} else if (document.selec tion.getRangeAt ) {
// 01-24-05 - this next bit may work in
Mozilla
var range =
document.select ion.getRangeAt( 0);
if (range) {
if (range.text) {
var fieldText = range.text;
} else if (range.value) {
var fieldText = range.value;
}
}
} else if (document.selec tion.toString) {
var fieldText =
document.select ion.toString();
}
} else if ('number' == typeof
myField.selecti onStart) {
// MOZILLA/NETSCAPE support
//
myField.value.s ubstring(myFiel d.selectionStar t,myField.selec tionEnd)
var startPos = myField.selecti onStart;
var endPos = myField.selecti onEnd;
if (myField.value. substring) {
if (endPos != 0) {
var fieldText =
myField.value.s ubstring(startP os, endPos);
}
}
}
if (fieldText != "") fieldValue = fieldText;
}

xt bit may work in Mozilla
var range =
document.select ion.getRangeAt( 0);
if (range) {
if (range.text) {
var fieldText = range.text;
} else if (range.value) {
var fieldText = range.value;
}
}
} else if (document.selec tion.toString) {
var fieldText =
document.select ion.toString();
}
} else if ('number' == typeof
myField.selecti onStart) {
// MOZILLA/NETSCAPE support
//
myField.value.s ubstring(myFiel d.selectionStar t,myField.selec tionEnd)
var startPos = myField.selecti onStart;
var endPos = myField.selecti onEnd;
if (myField.value. substring) {
if (endPos != 0) {
var fieldText =
myField.value.s ubstring(startP os, endPos);
}
}
}
if (fieldText != "") fieldValue = fieldText;
}

Jul 23 '05 #1
9 2219
lk******@geocit ies.com wrote:

I've got a function, you can see it below, that is being called
onmouseup in the textarea on my main form. The idea is to find a
selection if possible and store that text in a global variable. I can't
get this to work in any browser on a Mac, though it works alright on a
PC. What am I missing?

That document.select ion and element.selecti onStart don't work in
Safari or IE 5.2.

However, element.selecti onStart does work in Firefox on Mac but if you
select from the start of the text, selectionStart returns undefined so
your test:

if ('number' == typeof myField.selecti onStart) {

will fail.

Your method will also fail if the user selects all the text by clicking
in the field and uses Ctrl+A. It also stops double-clicking in the
field to select all the text.

The following play code works in Firefox 1.0 on Mac OS X (but not in
either Safari 1.0.3 or IE 5.2). It deals with selectionStart being
undefined if all the selection starts with the first character. Note
that if the user doesn't select anything, the start and end will have
the same value, and if the cursor is before the first character, both
selectionStart and selectionEnd will be undefined.

If the user selects some text, then clicks in the field, the function
will return the same value as if some text was still selected. I think
that qualifies as unreliable, but it's up to you.
<html><head><ti tle>play</title>
<script type="text/javascript">

function setFieldSelecti onValue(myField ) {

(document.selec tion)?
alert('document .selection works') :
alert('document .selection doesn\'t work');

var start, end;
if(myField.sele ctionStart || myField.selecti onEnd) {
if (myField.select ionStart) {
start = myField.selecti onStart
} else {
start = 0;
}
end = myField.selecti onEnd;
alert('Started at ' + start + '\nEnded at ' + end);
} else {
alert('selectio nStart and selectionEnd not supported');
}
}
</script>
</head><body>
<form action="">
<textarea onmouseup="
setFieldSelecti onValue(this);" >Fred is a very fine fellow</textarea>
</form>
</body></html>

--
Fred
Jul 23 '05 #2
Fred Oz wrote:
[...]
Your method will also fail if the user selects all the text by clicking
in the field and uses Ctrl+A. It also stops double-clicking in the
field to select all the text. [...] If the user selects some text, then clicks in the field, the function
will return the same value as if some text was still selected. I think
that qualifies as unreliable, but it's up to you.


This can be avoided by adding setFieldSelecti onValue as an
onclick to the submit button, or to any other button. As long
as the user clicks on the button immediately after making the
selection, it works.

The following code will report the selected range when either
"Show selection" or submit are clicked.

<html><head><ti tle>play</title>
<script type="text/javascript">

function setFieldSelecti onValue(myField ) {
var start, end;
if(myField.sele ctionStart || myField.selecti onEnd) {
if (myField.select ionStart) {
start = myField.selecti onStart
} else {
start = 0;
}
end = myField.selecti onEnd;
alert('Started at ' + start + '\nEnded at ' + end);
} else {
alert('selectio nStart and selectionEnd not supported');
}
}
</script>
</head><body>
<form action="" onsubmit="
setFieldSelecti onValue(this.fo rm.tx1);">
<textarea name="tx1">Fred is a very fine fellow</textarea>
<br>
<button onclick="
setFieldSelecti onValue(this.fo rm.tx1);
">Show selection</button><br>
<input type="submit" onclick="
setFieldSelecti onValue(this.fo rm.tx1);">
</form>
</body></html>

--
Rob
Jul 23 '05 #3
Does it occur to anyone else that the way to do this is to work with the
mouseup event in the textarea itself always updating a hidden field to
the current selection. Then no issues if user deselects before clicking
a button (cause the mouseup fires and resets selection to nothing) and
no issues with loss of focus (because mouseup isn't fired when the focus
is lost).???? I'm not gonna build a full-fledged example but it only
seems reasonable.
In article <41f7a7c0$0$105 54$5a62ac22@per-qv1-newsreader-
01.iinet.net.au >, rg***@iinet.net .auau says...
Fred Oz wrote:
[...]
Your method will also fail if the user selects all the text by clicking
in the field and uses Ctrl+A. It also stops double-clicking in the
field to select all the text.

[...]
If the user selects some text, then clicks in the field, the function
will return the same value as if some text was still selected. I think
that qualifies as unreliable, but it's up to you.


This can be avoided by adding setFieldSelecti onValue as an
onclick to the submit button, or to any other button. As long
as the user clicks on the button immediately after making the
selection, it works.

The following code will report the selected range when either
"Show selection" or submit are clicked.

<html><head><ti tle>play</title>
<script type="text/javascript">

function setFieldSelecti onValue(myField ) {
var start, end;
if(myField.sele ctionStart || myField.selecti onEnd) {
if (myField.select ionStart) {
start = myField.selecti onStart
} else {
start = 0;
}
end = myField.selecti onEnd;
alert('Started at ' + start + '\nEnded at ' + end);
} else {
alert('selectio nStart and selectionEnd not supported');
}
}
</script>
</head><body>
<form action="" onsubmit="
setFieldSelecti onValue(this.fo rm.tx1);">
<textarea name="tx1">Fred is a very fine fellow</textarea>
<br>
<button onclick="
setFieldSelecti onValue(this.fo rm.tx1);
">Show selection</button><br>
<input type="submit" onclick="
setFieldSelecti onValue(this.fo rm.tx1);">
</form>
</body></html>

Jul 23 '05 #4
--------------------
Does it occur to anyone else that the way to do this is to work with
the
mouseup event in the textarea itself always updating a hidden field to
the current selection.
-------------------

Interesting that you put it like that since that is what I thought I
was doing. Except that I was using a global var instead of a hidden
field. But the idea was the same, to store the info somewhere
onmouseup(), so as to avoid problems with loss of focus. That is how
the script works. The problem is that my function doesn't seem to be
capturing the info. Just to go over it again, my textarea gets declared
like this:

<div class="formInpu t"><textarea id="inputId5"
name="formInput s[cbMainContent]" style="height:2 00px; width:100%;"
class="textarea Input"
onmouseup="setF ieldSelectionVa lue(this)"></textarea>
<p><a href="#inputId5 " onclick="makeBi gger(inputId5); ">Make box
larger?</a></p></div>
and then setFieldSelecti onValue was trying to capture that value like
this:

// 01-24-05 - most of the time, with most browsers, you can't get a
selection from a form
// input with a function triggered with a form control like a
button, because clicking the
// button shifts the focus to the button and destroys the selection.
So I'm thinking that
// perhaps we can have a global variable that is set with
setFieldSelecti onValue() which
// can be called onMouseUp(). This sets the value and
insertAtCursor( ) can check to see
// if that value is there.
var fieldValue = "";

function setFieldSelecti onValue(myField ) {
// 01-24-05 - this next line is meant to ensure that we don't
// carry an old value forward.
fieldValue = "";

if (document.selec tion) {
if (document.selec tion.createRang e) {
var range = document.select ion.createRange ();
var fieldText = range.text;
} else if (document.selec tion.getRangeAt ) {
// 01-24-05 - this next bit may work in Mozilla
var range = document.select ion.getRangeAt( 0);
if (range) {
if (range.text) {
var fieldText = range.text;
} else if (range.value) {
var fieldText = range.value;
}
}
} else if (document.selec tion.toString) {
var fieldText = document.select ion.toString();
}
} else if ('number' == typeof myField.selecti onStart) {
// MOZILLA/NETSCAPE support
//
myField.value.s ubstring(myFiel d.selectionStar t,myField.selec tionEnd)
var startPos = myField.selecti onStart;
var endPos = myField.selecti onEnd;
if (myField.value. substring) {
if (endPos != 0) {
var fieldText = myField.value.s ubstring(startP os, endPos);
}
}
}
if (fieldText != "") fieldValue = fieldText;
}

Jul 23 '05 #5
John Doe wrote:
Does it occur to anyone else that the way to do this is to work with the
mouseup event in the textarea itself always updating a hidden field to
the current selection.
The original post was that it works OK on PC (presume Windows),
but not Mac. My (paraphrased) answer was that
document.select ion and element.selecti onStart do not work on Mac
for Safari and IE, and that Firefox has usability issues with
selections.
Then no issues if user deselects before clicking
a button (cause the mouseup fires and resets selection to nothing) and
no issues with loss of focus (because mouseup isn't fired when the focus
is lost).????
But other issues with using mouseup continue -

1. what if the selection is made using Ctrl+A?

2. using mouseup blocks double-clicking, a typical method of
selecting everything in the field.
I'm not gonna build a full-fledged example but it only
seems reasonable.


You should, then you'd know whether your suggestion works or
not.

--
Fred
Jul 23 '05 #6
This is for a software control panel, so I only need to get it working
in one Mac browser. I can tell users which browser to use. I'm dealing
with client in particular who is Mac based, and I'd like to get
something working for him this week. He's perfectly willing to use
whatever browser I suggest to him.

Jul 23 '05 #7
Much thanks to all your help. This is a good response and gives me
something to think about. I'm not sure I understand your concern here:

--------------------------
If the user selects some text, then clicks in the field, the function
will return the same value as if some text was still selected. I
think
that qualifies as unreliable, but it's up to you.
--------------------------

That is sort of the idea. Reading through Danny Goodman's book on
Javascript, I noticed that one can grab selections using Javascript,
but one usually can't manipulate those selections by clicking on form
inputs, because clicking a button shifts the focus and therefore
deselects the text (the exceptions are FireFox and IE on PC). I was
trying to think of ways around this, and I think someone here suggested
capturing selected text onmouseup(). It occured to me I could store the
selection in a global var, and then use that when someone clicks a
button. The idea is to wrap the text in an HTML tag, for instance, a
bold tag. The global fieldValue can be used as a last ditch resort if
the current browser is not FireFox or IE on a PC. In the following
function, we hope to be dealing with FireFox of IE on a PC, but if not,
at the end, we use whatever value has been stored to fieldValue. You
mention that this method of doing things is unreliable, if so, I'm
thinking that maybe another way to do this would be to use a prompt as
the last ditch effort, and ask the user to input whatever text they
want wrapped by the HTML tag they clicked on.

function insertAtCursor( myField, tag) {
// 01-24-05 - this first if() block is for Microsoft IE on PC
if (document.selec tion && document.select ion.createRange ) {
var range = document.select ion.createRange ();
if (range.text == '') {
alert("You haven't selected any text to change.");
} else {
range.text = '<' + tag + '>' + range.text + '<\/' + tag + '>';
}
} else if ('number' == typeof myField.selecti onStart) {
// this next bit is for Netscape/Mozilla
//
myField.value.s ubstring(myFiel d.selectionStar t,myField.selec tionEnd)
var startPos = myField.selecti onStart;
var endPos = myField.selecti onEnd;
if (endPos != 0) {
var mySelection = myField.value.s ubstring(myFiel d.selectionStar t,
myField.selecti onEnd);
mySelection = '<' + tag + '>' + mySelection + '<\/' + tag + '>';
myField.value = myField.value.s ubstring(0, startPos) +
mySelection + myField.value.s ubstring(endPos , myField.value.l ength);
} else {
alert("You haven't selected any text to change.");
}
} else {
// 01-24-05 - the above methods work in IE and Firefox on the PC.
For other
// browsers we try to use the fieldValue variable and append to
the end of the
// textarea. fieldValue is global and set onmouseup by
setSelectionFie ldValue().
myField.value += '<' + tag + ' >' + fieldValue + '<\/' + tag +
'>';
}
}

Jul 23 '05 #8
We've set up a site as a demo site and a debugging site. You can see
what I'm trying to do here:

http://www.publicpen.com/designer/mcControlPanel.php

You'll need a username and password to get in. Use these:

username: designer
password: designer123

In invite everyone to go kick it around.

When you log in, you'll see a text area on the lower right side of the
screen. There are some formatting buttons there. The way it works in IE
and FireFox on a PC is you can select text and then it a formatting
button (like bold) and the javascript will wrap the text in some HTML
tags. I'm trying to get this to work in at least one browser on the
Mac.

Jul 23 '05 #9
lk******@geocit ies.com wrote:
We've set up a site as a demo site and a debugging site. You can see
what I'm trying to do here:

http://www.publicpen.com/designer/mcControlPanel.php

You'll need a username and password to get in. Use these:

username: designer
password: designer123

In invite everyone to go kick it around.


Once logged in with Firefox, clicking on a top menu items gives:

"Error: document.getEle mentById("mainP anel") has no properties
Source File:
http://www.publicpen.com/designer/mc...p?arrangement=
Line: 983"
You have a div with class="mainPane l", but none with that id.

The navigation seems hopeless, probably because of the failure of
the above. Anyway, eventually I got to a page with a text area
and buttons to format entered text.

It seems to work OK in Firefox for Mac OS X. Personally, I think
attempting an HTML editor this way is pointless. It is easy to
ceate crap, like <h5>blah<h5></h5></<h5>h5></<h5></h5>h5>.

Unless you are goning to parse and validate the HTML, why bother?

The markup you are adding is trivial to learn, but hey, it's your
page.
--
Fred
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1818
by: Greener | last post by:
Hi, I need help badly. Sorry for my ignorance! What's the comparable file in .js to Global.asa in ASP to capture the browser version (IE vs. Netscape)? Any existing code to share or if you have a better way to do it without using any server-side programming? Can you write: if (navigator.userAgent.indexOf("MSIE") != -1) as Client-side...
2
7084
by: tmaster | last post by:
I can detect a right click on my treeview, but SelectedNode.Index points to the last node that was left-clicked. Is there a way for a right-click event of a treeview to update the SelectedNode.Index? Private Sub tvwTopics_Mouseup(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvwTopics.MouseUp If e.Button =...
2
4671
by: dave.wayne | last post by:
In a web page I have a div tag that has a onlick event registered through the event listener. However, that same div tag also has a onmousedown - start a drag and drop script The problem I am having is that once the drag and drop is complete, the mouse button is released and the onclick event is firing. I've tried returning false from the...
5
70698
by: den2005 | last post by:
Hi everybody, Problem with dragging effect of resizing is working but having problem with setting a flag to determine if mouse button is click or not. Is there anyone knows how to fixed this? I used several ways to do this as shown below, all telling me the error below. I used IE 6 w/ SP1. The error comes from onmousedown"mousedown()". ...
4
9008
by: Hardy Wang | last post by:
Hi all, In order to solve code-behind of global.asax problem, I removed the code from global.asax, and just leave one line "<%@ Application Language="C#" Inherits="Global"%>" in this file. Then I created a CS file in App_Code folder named Global.asax.cs: public class Global : System.Web.HttpApplication { void Application_OnError(object...
1
1713
by: paulo | last post by:
Hello, I have a DLL library containing some web services which are declared in each .asmx file in the following way: <%@ WebService Language="C#" Class="LibraryName.WebService" %> I would like to log every unhandled exception that occurs on any web service declared in the library to a file. I noticed the global class, usually inside...
5
1397
by: Eric Layman | last post by:
Hi everyone, Currently Im using Global.asax.vb to capture any errors and to send them via email to me. But I didn't get any emails when I purposely crash the web application. May I know what I am missing here? I've copied (shamelessly) the script from here:...
0
2930
by: snehal12345 | last post by:
I have created 1 contextmenu for a listbox in C#.net 3.0 . For that context menu I have written one event handler for mouseup event . But when i click on contextmenu item that event is not captured. I have done following: { /*in constructor of main window */ listbox1.ContextMenu.Items.Add("save"); listbox1.ContextMenu.MouseUp += new...
1
2746
by: destiny007 | last post by:
can any one help me to write code to capture selected text from a page but problem is that i am not able to decide where to call the functio.in this case the function is called in all cases of mouse out. here is the code <html> <HEAD> <SCRIPT LANGUAGE="JavaScript"> var captured = ""; function captext() {
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.