/    Sign up×
Community /Pin to ProfileBookmark

My script won’t work in IE but works fine in Firefox

I came up with this script to insert a text before and after a selected text in a textarea (a button calls the fuction which contains this):


___________________________________________

var txtarea = document.post.message;

var Length = txtarea.textLength;
var Start = txtarea.selectionStart;
var End = txtarea.selectionEnd;

var s1 = (txtarea.value).substring(0,Start);
var s2 = (txtarea.value).substring(Start,End)
var s3 = (txtarea.value).substring(End,Length);

txtarea.value = s1 + “text before selection” + s2 + “text after selection” + s3;


____________________________________________

unfortunately this works in Firefox but not in IE. I assume IE does not recognize .textLength .selectionStart .substring

can you please tell me an alternative way to refer to my selection so that both IE and Firefox will understand it?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@ExuroJan 16.2005 — This worked for me:
[code=php]function addText() {
var txtarea = document.getElementById("txt1");
var preString = "<", postString = ">";
if (txtarea.setSelectionRange) { // Mozilla/Firefox
var Start = txtarea.selectionStart;
var End = txtarea.selectionEnd;
var s1 = (txtarea.value).substring(0,Start);
var s2 = (txtarea.value).substring(Start,End)
var s3 = (txtarea.value).substring(End);
txtarea.value = s1 + preString + s2 + postString + s3;
}
else if (txtarea.createTextRange) { // Internet Explorer
txtarea.focus();
document.selection.createRange().text = preString + document.selection.createRange().text + postString;
}
else { // Other
txtarea.value = preString + txtarea.value + postString;
}
}[/code]

[i]Edit:[/i]

Removed the unneeded [FONT=courier new]Length[/FONT] variable
Copy linkTweet thisAlerts:
@amazing_andr3authorJan 16.2005 — I have stumbled upon the same thing some time after I posted the thread. It seems there is no one way of doing this, so I need two versions of the script. Here's the part for IE, which is almost exactly what you suggested.

else if (document.selection)

{

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

if (theSelection.length > 0)
{
document.selection.createRange().text = tags[tag] + theSelection + tags[tag+1];
txtarea.focus();
theSelection = '';
}
}

}

unfortunately trying to refer to one textarea only as in:

document.getElementById('form').mytextarea.selection.createRange().text

doesn't seem to work. So now the person using the form can insert things like <a> on the <input> for the TITLE of his post, which is not really something I want to allow.

Of course I can filter the tags out of these <inputs>, but it would have been much cleaner if people couldn't insert them in the first place.
Copy linkTweet thisAlerts:
@ExuroJan 16.2005 — [i]Originally posted by amazing_andr3 [/i]

[B]doesn't seem to work. So now the person using the form can insert things like <a> on the <input> for the TITLE of his post, which is not really something I want to allow.[/B][/QUOTE]

Just use your server-side language to replace all instances of < with &amp;lt; and > with &amp;gt; in the input fields.
×

Success!

Help @amazing_andr3 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...