 |
 |
Help with Firefox and Safari?
|
 |
|
 |
|
Junior Member
Join Date: Sep 2002
Location: Scottsdale
Status:
Offline
|
|
'ello...
I've been toying with this javascript to make selected text a link and it's only working in IE on the PC (haven't checked safari yet...will when I get home). In Firefox I think it's because when you click "Add Link" (which is a text link), that gets focus and the focus is never going back to the textarea (which it should).
Anyone know what's up?
Code:
function GetSelectedText(elementID) {
var txt = '';
if (window.getSelection) {
txt = window.getSelection();
} else if (document.getSelection) {
txt = document.getSelection();
} else if (document.selection) {
txt = document.selection.createRange().text;
}
else return;
var n = prompt("Enter the URL","http://");
if(n) {
// replace the variable with the html
txt = "<a href=\"" + n + "\" target=\"_blank\">" + txt + "</a>";
SetSelectedText(elementID, txt);
} else {
alert("Error : no link provided");
}
}
function SetSelectedText(elementID, text) {
var obj = document.getElementById(elementID);
if( !obj )
throw "Element " + elementID + " not found!";
// Netscape, Mozilla, Firefox
if( typeof(obj.selectionStart) != "undefined" ) {
obj.focus();
var start_selection = obj.selectionStart;
var end_selection = obj.selectionEnd;
if (end_selection < start_selection){
var temp = end_selection;
end_selection = start_selection;
start_selection = temp;
}
var startText = (obj.value).substring(0, start_selection);
var endText = (obj.value).substring(end_selection, obj.textLength);
obj.value = startText + text + endText;
obj.selectionStart = start_selection;
obj.selectionEnd = start_selection + text.length;
}
// Internet Explorer
if(obj.createTextRange) {
obj.focus( obj.caretPos );
obj.caretPos = document.selection.createRange().duplicate();
obj.caretPos.text = text;
obj.caretPos.moveStart( "character", text.length * -1 );
obj.caretPos.select();
}
obj.focus();
}
|
|
Jay Contonio
http://www.jcontonio.com/
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |