473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Making a MS jscript ECMA/DOM compliant

Now what I have is a function that only works in WinIE4+ because it's
uses Microsoft proprietary properties and methods:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function gmshortcutkeys() {
  3. if ((parseInt(navigator.appVersion) >= 4) && (navigator.appName ==
  4. "Microsoft Internet Explorer")) {
  5. if (event.ctrlKey != true) return;
  6. gmselection = document.selection.createRange().text;
  7. if (window.event.keyCode == 1) {
  8. gminsertlink = prompt("What do you want to link to?",
  9. "http://")
  10. if (gminsertlink == null) return;
  11. document.selection.createRange().text = '<a href="' +
  12. gminsertlink + '">' + gmselection + '</a>';
  13. return;
  14. }
  15. if (window.event.keyCode == 2) {
  16. document.selection.createRange().text = '<strong>' +
  17. gmselection + '</strong>';
  18. return;
  19. }
  20. if (window.event.keyCode == 3) {
  21. gminsertacrotitle = prompt("How is this acronym spelled out?",
  22. "Start here")
  23. if (gminsertacrotitle == null) return;
  24. document.selection.createRange().text = '<acronym title="' +
  25. gminsertacrotitle + '">' + gmselection + '</acronym>';
  26. return;
  27. }
  28. if (window.event.keyCode == 4) {
  29. gminsertdfntitle = prompt("How is this jargon or technical
  30. term defined?", "Start here")
  31. if (gminsertdfntitle == null) return;
  32. document.selection.createRange().text = '<dfn title="' +
  33. gminsertdfntitle + '">' + gmselection + '</dfn>';
  34. return;
  35. }
  36. if (window.event.keyCode == 5) {
  37. gminsertdelcite = prompt("Point to the page responsible for
  38. this deletion.", "http://")
  39. if (gminsertdelcite == null) return;
  40. document.selection.createRange().text = '<del datetime="' +
  41. document.lastModified + '" cite="' + gminsertdelcite + '">' +
  42. gmselection + '</del>';
  43. return;
  44. }
  45. }
  46. }
  47.  
  48.  
This function HTML tags around selected text in a textarea for a CMS.
It does this in response to keyboard events thus making a simplistic,
through-the-web markup editor for textareas. For example, you select
some text in the textarea, hit a key combo and bang, your select text
is bounded by <strong> tags. Occasionally it may hit the user with a
prompt to fill in a value for the tag attribute.

Kinda quick and nifty huh?

The problem is it only works in Internet Explorer. What I'd like is
some suggestions on how to change this script so that it works in
Mozilla1, Firefox and heck maybe even Opera 7.5 or Safari. I am
completely willing to sacrifice backward compatibility with anything
less than IE6.

I'd like suggestions that bring this function into close compliance
with the W3C's DOM/ECMA rec but if I must use a little proprietary
stuff for it work in the above browsers, I'm okay with that.

Any suggestions?

Jul 23 '05 #1
4 1857


Next Generation Technologies Inc. wrote:

This function HTML tags around selected text in a textarea for a CMS.
It does this in response to keyboard events thus making a simplistic,
through-the-web markup editor for textareas. For example, you select
some text in the textarea, hit a key combo and bang, your select text
is bounded by <strong> tags. Occasionally it may hit the user with a
prompt to fill in a value for the tag attribute. The problem is it only works in Internet Explorer. What I'd like is
some suggestions on how to change this script so that it works in
Mozilla1, Firefox and heck maybe even Opera 7.5 or Safari.


Mozilla (since 1.3) has its own API to manipulate the selection in a
HTML textarea control, see
<http://www.faqts.com/knowledge_base/view.phtml/aid/13562/fid/130>
I think Opera has nothing for that so far. Don't know about Safari.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
I don't know about Opera but Mozilla provides:

- 1. the selectionStart and selectionEnd properties on a textarea, plus
the setSelectionRange() method;

- 2. most of the Range portion of W3C's "Document Object Model (DOM)
Level 2 Traversal and Range Specification"
(http://www.w3.org/TR/DOM-Level-2-Traversal-Range), in which, for example,

document.selection.createRange().text
becomes:
window.getSelection().getRangeAt(0).toString()

Don't overlook any simple answers, though: I still find the best way to
add stuff in the middle of a textarea is

var obj = document.forms[0].textarea1;
var str = obj.value;
obj.value = str.substring(0, 5) + "<newtag/>" + str.substring(6);

For events, Mozilla uses the W3C standard
(http://www.w3.org/TR/DOM-Level-2-Events), in which you starting
intercepting events with addEventListener([type], [function], [begin]).
Jul 23 '05 #3
Martin,

Well I'd rather avoid Mozilla's API if it is proprietory. What I want
is a cross-platform, DOM-compliant way to do what the script above
does.

But if I can't have that, I guess I could do a sniff and route the
client to the method that works for their browser.

Jul 23 '05 #4
Paul,
var obj = document.forms[0].textarea1;
var str = obj.value;
obj.value = str.substring(0, 5) + "<newtag/>" + str.substring(6);


I don't quite understand this here. Doesn't this code seem to require
that I already explicitly know the length of the string I want to
bound?

What I want is:

1) To dynamically select a string with my mouse or with the keyboard
2) Hit a key combo and have JavaScript bound the selected string with
the HTML tags associated with that key combo.

Basically the code I cited at that start of this thread is a
simple-minded, non-WYSIWYG, keyboard driven markup editor of sorts that
I use with my blogging tool. I've moved to Firefox and the above code
no longer works.

Jul 23 '05 #5

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

Similar topics

4
by: Harag | last post by:
Hi All I currently thinking of converting from my little knowledge of VBscript to jScript ASP. With this in mind I'm looking at my current code to see how it will convert over to Jscript. ...
14
by: John Bentley | last post by:
Note this is crossposted to comp.lang.javacript and microsoft.public.dotnet.scripting. After some Googling and FAQing my understanding of these terms is, crudely: Javascript (3 different...
10
by: Gernot Frisch | last post by:
Hi, I have found some menu functions. It works quite well, but how can I replace it with a simple <a href> if javascript is turned off? I reduced my code to:...
10
by: Danny | last post by:
In the classic asp, I can use escape built-in function (server side function) like this: <script language=javascript> var myContent = unescape(<%=escape(strContent)%>) </script> How do I do...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
28
by: Andre | last post by:
Hi, Does anyone know whether the ECMA, or an other standard document, specifies a maximum for the value that can be pass to the setTimeOut() function in Javascript? Andre
37
by: pochartrand | last post by:
Hello, Is there a way to manipulate or at least read the pseudo-class of an element ? I know the style attribute can be accessed and gives the CSS properties for a particular element....
10
by: the DtTvB | last post by:
I heard it is not the same. The lastest version of JavaScript is 1.6 while lastest version of JScript is 8.0 I also heard a little bit about ECMAScript, but I still don't know what's the...
4
by: Lando | last post by:
I'm a bit confused ...javascript and jscript are the same thing ? javascript and java are related.......... javascript in windows differs from javascript in linux....? Sorry,... if some question...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
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...
0
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...
0
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...

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.