View Single Post
Old 11-20-2012, 11:42 AM   PM User | #16
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
update

Quote:
Originally Posted by rnd me View Post
look no further:

Code:
function selHTML() {

    if (window.ActiveXObject) {
        var c = document.selection.createRange();
        return c.htmlText;
    }

    var nNd = document.createElement("p");
    var w = getSelection().getRangeAt(0);
    w.surroundContents(nNd);
    return nNd.innerHTML;
}
i noticed this wasn't working in chrome all the time; specifically if the range start and endpoints have different containers. there's a destructive version that works posted, but i thought i would share my harmless and fast update.
it also absolute-izes the hrefs and src, so your selected html will work anywhere it's pasted.

Code:
function selHTML() {

    if (window.ActiveXObject) {
        var c = document.selection.createRange();
        return c.htmlText;
    }
	var x=document.createElement("div"); 
	x.appendChild(getSelection().getRangeAt(0).cloneContents()); 
	[].slice.call(x.querySelectorAll("[src],[href]"))
		.forEach(function(a){a.href=a.href; a.src=a.src;;});
   return x.innerHTML;
}
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote