Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Help with Firefox and Safari?

Help with Firefox and Safari?
Thread Tools
Junior Member
Join Date: Sep 2002
Location: Scottsdale
Status: Offline
Reply With Quote
Jun 17, 2005, 06:45 PM
 
'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/
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:16 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2