Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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,550
Thanks: 9
Thanked 479 Times in 462 Posts
rnd me is a jewel in the roughrnd 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:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting 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

Forum Jump


All times are GMT +1. The time now is 07:27 AM.


Advertisement
Log in to turn off these ads.