Hi all...
I am able to grab the text that a user has selected on a web page,
using this code:
function moreInfo() {
if (!isIE) {
var t = window.getSelec tion();
// act on variable "t";
} else if (document.selec tion && document.select ion.createRange ) {
var t = document.select ion.createRange ();
if (document.selec tion.type == 'Text' && t.text>'') {
document.select ion.empty();
// act on variable "t";
}
}
}
(the boolean "isIE" is established earlier in the code)
This works. However, I'm wondering how to get the words *around* the
selected word. Take these sentences for example: "If you need to
framglubble the zartbox, then you should buy the red widget.
Otherwise you can buy the blue widget and save some money."
My code will tell me if the person has selected the word "widget".
But I'd like to know if the selection is after "red" or "blue". Is
this possible? I've been scouring the Internet for some advice, and
I'm having trouble finding an answer.
Many thanks in advance for your time. 3 2546
On Feb 22, 7:39 pm, "Dekortage" <dekort...@gmai l.comwrote:
My code will tell me if the person has selected the word "widget".
But I'd like to know if the selection is after "red" or "blue". Is
this possible?
Not in any straightforward way AFAIK.
You may extend/shift range left or right to see what words will be
added. In any case it presumes some fixed content you know in advance
so able to recognize by fragments. In this case wouldn't it be much
more easy to mark all needed segments in advance? Just a suggestion:
<p>If you need to framglubble the zartbox, then you should buy the
<span onclick="f(this )">red widget</span>.
Otherwise you can buy the <span click="f(this)" >blue widget</spanand
save some money.</p>
On Feb 22, 3:36 pm, "VK" <schools_r...@y ahoo.comwrote:
You may extend/shift range left or right to see what words will be
added. In any case it presumes some fixed content you know in advance
so able to recognize by fragments. In this case wouldn't it be much
more easy to mark all needed segments in advance? Just a suggestion:
<p>If you need to framglubble the zartbox, then you should buy the
<span onclick="f(this )">red widget</span>.
Otherwise you can buy the <span click="f(this)" >blue widget</spanand
save some money.</p>
Thanks for your response, VK.
I hear ya. I have specific reasons for doing it the other way. I'm
trying to develop a definition system so someone can select text on a
page and hit button to get a definition. Using the example above,
they could select "widget" or "framglubbl e" or "money". But there are
many words which have different meanings depending on context, so the
definition system could be more accurate/useful if I could
intelligently scan context.
I'm intrigued about shifting the range to the left. I expect to be
working with fairly fixed content (or at least indexable content that
would be intelligently tied to the definition system). How would I do
that?
Okay. I have a really rough, unoptimized version of what I want. The
IE part is pretty simple, since it can move the selection boundary
range around. The Mozilla part is a bit more of a hack: I get the
full text of the node that the selection resides in, then based on the
selection offset within the node, I chop up the text around it to
determine the words just before and after the selected word. It
doesn't work quite perfectly, but it's close. It doesn't work with
Safari but I've tested it succesfully with Firefox (Mac) and IE 6
(Windows). (Safari has some weird text range issues...?!?)
Below is the code for an entire HTML page. Hopefully the word wrap
won't kill it. I freely admit it is an ugly, unoptimized hack thrown
together as a proof of concept, so you're welcome to clean it up, fix
bugs, add features, etc.
<html>
<head>
<title>Framglub ble the Zartbox: getting selection context via
JavaScript</title>
<script language="javas cript">
// Original author: Dekortage @ a server called Gmail.com
// You're free to copy this but leave the "Original author" line
intact.
//
// This deliberately returns a "null" value if it determines that the
previous
// or next word is separated from the selected word by a comma or
period.
// determine browsers
var isIE = document.all;
var isN6 = document.getEle mentById && !document.all;
var isN4 = document.layers ;
var detect = navigator.userA gent.toLowerCas e();
var isSafari = ( detect.indexOf( "safari") 0 );
function trim(x) { return x.replace(/^\s*|\s*$/g,''); }
function moreInfo() {
if (!isIE) {
var t = window.getSelec tion(); // selected text
tx = t+"";
preWord = null;
postWord = null;
if (!isSafari) { // Safari does not work with this
origNode = t.anchorNode;
orig = origNode.textCo ntent + "";
// get previous word
preTxt = orig.slice(0,t. anchorOffset);
preArray = preTxt.split(" ");
preCount = preArray.length ;
while ((preWord == null) && (preCount >= 0)) {
preCount -= 1;
if ((preArray[preCount] != null) && (preArray[preCount] != "") &&
(preArray[preCount] != " ")) {
preWord = preArray[preCount];
if ((preWord.charA t(preWord.lengt h-1) == ",")) preWord = null;
}
}
// get next word
postTxt = orig.slice((t.a nchorOffset + tx.length + 1));
postArray = postTxt.split(" ");
if ((postArray[0] != ".") && (postArray[0] != ",") &&
(postArray[0] != "--") && (postArray[0] != "") &&
(postArray[0] != " ")) { // if not end of sentence
postWord = postArray[0];
}
}
showWords(tx,pr eWord,postWord) ;
} else if (document.selec tion && document.select ion.createRange ) {
var t = document.select ion.createRange (); // selected text
if ((document.sele ction.type == 'Text') && (t.text '')) {
tx = trim(t.text);
t.moveStart("wo rd",-1); // not sure why t.move() alone wouldn't work
t.moveEnd("word ",-1);
preWord = trim(t.text);
if ((preWord == ",") || (preWord == ".")) {
preWord = null;
}
t.moveEnd("word ",2);
t.moveStart("wo rd",2);
postWord = trim(t.text);
if ((postWord == ",") || (postWord == ".")) {
postWord = null;
}
// need to fix this to work with hyphenated words
document.select ion.empty();
showWords(tx,pr eWord,postWord) ;
}
}
}
function showWords(t,a,z ) { window.alert("S equence: "+a+","+t
+","+z); }
if (isIE) document.ondblc lick=moreInfo;
</script>
</head>
<body ondblclick="if (!isIE) moreInfo()" style="font-size: 24px;
margin: 24px;">
<p>If you need to framglubble the zartbox, then you should buy the red
widget. Otherwise you can buy the blue widget and save some money.</p>
<p>(double-click on a word to see its context)</p>
</body>
</html> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: worzel |
last post by:
Can't suss this out for the life of me, googling of no help either.
Okay, new to win forms; I have a treeview with several root nodes, each
having a single level of child nodes underneath. I also havea context
(popup) menu which, what I want, is for it to pop up with certain options
(menuItems) when a root treeNode is selected and different...
|
by: gooderthanyou |
last post by:
I have a textfield and you of course you can select text...
When they hit the bold button I want it to obtain the selected text and
bold it, the hard part is trying to figure out if javascript can even
OBTAIN selected text?!?!
I can do this in java if I have to, I know java better than javascript,
it seems as that is the only way to do it...
|
by: brisco5 |
last post by:
I have a TEXTAREA element. A user right clicks within in to get the
context menu and they select "paste". I want my javascript code to
know that they selected "paste".
I know you can capture the mouse click, but can we capture exactly what
event that attempted?
Thanks,
Mike
|
by: paolol |
last post by:
Hi,
I wont to get the current row values from a dataset, any one know how to
reach those data ??
At the moment I use a work around, but it's not good ..
foreach (DataRow row in this.pitagoralavoriDataSet.Addetti.Rows)
{
if (Convert.ToString(row)== sNomeU){
idutente = Convert.ToInt32(row);
}
|
by: Jukka K. Korpela |
last post by:
I recently noticed, once again, how the common implementation of italics can
really disturb. I'm referring to the common phenomenon that there is by
default too little spacing after italics text, so that if you have, say,
<em>Bill</emWatterson
then the last "l" of "Bill" hits the "W" - they may even slighly overlap. I
noticed this long ago,...
| |
by: travelrats |
last post by:
Hi
Here's my problem - I've been trying to figure this out for a while and ran out of ideas...
<div> id1 contains <div> id2 (a geographical map)
<div> id2 contains a context menu made out of an <ul> list. the context menu operations work fine, links are hovered, etc
When I click one context menu items named "view large", <div> id2 is moved...
|
by: active |
last post by:
In control panel/Display/Appearance/Effects if :
'Use the following method to smooth edges of screen fonts' is checked and
ClearType is selected in the combobox (no problem if Standard is selected)
I get a crazy font display in parts of my picturebox.
|
by: Karl |
last post by:
Hi all,
I want to write an application that is launched from the context menu
in Windows Explorer/Computer. That is to say, when I am browsing
around my hard drive and get to any location I choose, I want to be
able to select several files, right click my mouse and launch an
application which will act on the selected files.
However, I...
|
by: divya1949 |
last post by:
Create a windows c# application which will
Read a xml file and populate nodes in the treeview.
1 On selection of treenode display the child nodes of that node in listview control
2. Provide following view properties to listview, through View menu
a. Tile
b. Icon
|
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. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| | |