Forum Moderators: open
Yes, am new and already have a problem. I'm writing a Javascript *sniff*, something for my blog and encountered a quite serious issue: i can't find an IE workaround for the Javascript insertnode.
Basically what I want to do is to append a new span element right after a user's highlighted text. For example John Doe comes to my site, highlights a word, and right after his highlighted word a new span element appears. I know it can be done using prototype and possibly other frameworks as well, but please tell me I don't have to use a framework in order to achieve the desired effect.
Currently I have the following working code for Mozilla, Safari and Chrome:
selectionButton = document.createElement('span');
selectionButton.className = 'selection';
selectionButton.id = 'selection_id';
var range = selection.getRangeAt(0);
newRange = document.createRange();
newRange.setStart(selection.focusNode, range.endOffset);
newRange.insertNode(selectionButton);
document.getElementById("selection_id").addEventListener("mousedown",defunct,false);
Would somebody help me please?! I don't want to rob anyone's precious time so no need for code snippet, even the right direction I should go further should do it.
Thanks in advance
For further reference: If you need walk around for for the W3C insertnode in IE, used with user selection:
selectionButton = document.createElement('span');
selectionButton.className = 'selection';
selectionButton.id = 'selection_id';if(window.attachEvent) {
var sel = theSelection();
var tmp = document.createElement('div');
tmp.appendChild(selectionButton);
newRange = sel.duplicate();
newRange.setEndPoint( "StartToEnd", sel);
newRange.pasteHTML(tmp.innerHTML);
}else{
var range = selection.getRangeAt(0);
newRange = document.createRange();
newRange.setStart(selection.focusNode, range.endOffset);
newRange.insertNode(selectionButton);
}
where theSelection() is a crossbrowser function which creates the selection, object(!) in IE's case, text in all the other browsers.
if(window.attachEvent) is a weak attempt to detect whether the browser is IE. tmp is just a placeholder I used to transport the selection.
I feel kinda special :)
Found the best solution though:
var isMSIE = /*@cc_on!@*/false; IE uses JScript which will understand the above as: isMSIE = !false, aka TRUE. All the other browsers on the other hand can't do anything with the /*@cc_on!@*/ comment so it will be isMSIE = false.
Cool.
Anyway, I'm used with the fact that I talk with myself, sometimes I answer, too. It's just me...
Again, thanks for your input.
BTW, funny is that YUI has no idea what to do with the declared variable
There are lots of versions of IE, past and future with different 'features' plus quirk modes. Much better to be not concerned with specific browsers, test if best way to do task is available then provide fallbacks, that's more bullet-proof, difference is overhead is minimal.
BTW, did you see yet IE8? Even though MS states that they changed the whole thing whatsoever, I don't see too much difference :¦
Thanks for the welcome