Making a MS jscript ECMA/DOM compliant

  • Thread starter Next Generation Technologies Inc.
  • Start date
N

Next Generation Technologies Inc.

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

Code:
function gmshortcutkeys() {
if ((parseInt(navigator.appVersion) >= 4) && (navigator.appName ==
"Microsoft Internet Explorer")) {
if (event.ctrlKey != true) return;
gmselection = document.selection.createRange().text;
if (window.event.keyCode == 1) {
gminsertlink = prompt("What do you want to link to?",
"http://")
if (gminsertlink == null) return;
document.selection.createRange().text = '<a href="' +
gminsertlink + '">' + gmselection + '</a>';
return;
}
if (window.event.keyCode == 2) {
document.selection.createRange().text = '<strong>' +
gmselection + '</strong>';
return;
}
if (window.event.keyCode == 3) {
gminsertacrotitle = prompt("How is this acronym spelled out?",
"Start here")
if (gminsertacrotitle == null) return;
document.selection.createRange().text = '<acronym title="' +
gminsertacrotitle + '">' + gmselection + '</acronym>';
return;
}
if (window.event.keyCode == 4) {
gminsertdfntitle = prompt("How is this jargon or technical
term defined?", "Start here")
if (gminsertdfntitle == null) return;
document.selection.createRange().text = '<dfn title="' +
gminsertdfntitle + '">' + gmselection + '</dfn>';
return;
}
if (window.event.keyCode == 5) {
gminsertdelcite = prompt("Point to the page responsible for
this deletion.", "http://")
if (gminsertdelcite == null) return;
document.selection.createRange().text = '<del datetime="' +
document.lastModified + '" cite="' + gminsertdelcite + '">' +
gmselection + '</del>';
return;
}
}
}

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

Martin Honnen

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

NGT Inc.

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

NGT Inc.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top