PermaLink BlogSphere JavaScript problem with UUB code buttons in Firefox browser02/27/2005 02:37 PM

As I was reporting here, the buttons to format selected text using the JavaScript buttons on the BlogSphere comments form did not work when using the Firefox browser.
To find the problem source I first tried to find out what's going on using Venkman,  a powerful JavaScript debugger for Firefox. I figured out that the following function was beeing called, but returned immediately as document.selection is not available in Firefox.

function formatStr (v) {
  if (!document.selection) return;
  var str = document.selection.createRange().text;
  if (!str) return;
  document.selection.createRange().text = '[' + v + ']' + str + '[/' + v + ']';
  ReloadTextDiv();
}


This is a known issue and I have found it beeing reported
here. As I do not like to write new code, I searched my favorite HTML guide SelfHTML and found this great article which helped me to fix the problem as it covers the same function for several browsers. I renamed some elements and got it working with BlogSphere. The new code is much longer as the old one:

function formatStr (v) {
var aTag =  '[' + v + ']';

var eTag =  '[/' + v + ']';

var input = document.forms['StoryResponse'].elements['Body'];
input.focus();
/* für Internet Explorer */
if(typeof document.selection != 'undefined') {
  /* Einfügen des Formatierungscodes */
  var range = document.selection.createRange();
  var insText = range.text;
  range.text = aTag + insText + eTag;
  /* Anpassen der Cursorposition */
  range = document.selection.createRange();
  if (insText.length == 0) {
    range.move('character', -eTag.length);
  } else {
    range.moveStart('character', aTag.length + insText.length + eTag.length);      
  }
  range.select();
}
/* für neuere auf Gecko basierende Browser */
else if(typeof input.selectionStart != 'undefined')
{
  /* Einfügen des Formatierungscodes */
  var start = input.selectionStart;
  var end = input.selectionEnd;
  var insText = input.value.substring(start, end);
  input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
  /* Anpassen der Cursorposition */
  var pos;
  if (insText.length == 0) {
    pos = start + aTag.length;
  } else {
    pos = start + aTag.length + insText.length + eTag.length;
  }
  input.selectionStart = pos;
  input.selectionEnd = pos;
}
/* für die übrigen Browser */
else
{
  /* Abfrage der Einfügeposition */
  var pos;
  var re = new RegExp('^[0-9]{0,3}$');
  while(!re.test(pos)) {
    pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
  }
  if(pos > input.value.length) {
    pos = input.value.length;
  }
  /* Einfügen des Formatierungscodes */
  var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
  input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
}

 ReloadTextDiv();
}



This page has been accessed 1634 times. .
Disclaimer
The weblog represent my personal views and comments and does not represent the views of my current or previous employers or customers.
About me
By Category
The BlogRoll
XING advertisement
Proudly Employed By
IBM Lotus Logo
Buy this book
Search
Monthly Archive
2012
2011
2010
2010
2010
2010
2009
2009
2009
2009
2009
2009
2009
2009
2008
2008
2008
2008
2008
2008
2008
2007
2007
2007
2007
2007
2007
2007
2007
2007
2007
2006
2006
2006
2006
2006
2006
2006
2006
2006
2006
2006
2006
2005
2005
2005
2005
2005
2005
2005
2005
2005
2005
2005
2004
2004
2004
2004
2004
2004
2004
2004
2004
2004
2004
2004
2003
2003
Full Archive
Todays Referrers
Advertisement