javascript - Is there a way to better way to get outerHtml? - Stack Overflow

I'm trying to get a consistent, cross browser outerHtml for a jQuery element, particularly $("

I'm trying to get a consistent, cross browser outerHtml for a jQuery element, particularly $("html").outerHtml(). For example if the page source is this:

<html><script src="blah.js"></script><div class="foo"   id='bar'></p></div></html>

I want to be able to use $("html").outerHtml(); to get the HTML string including the element itself, which would be:

<html><script src="blah.js"></script><div class="foo" id="bar"><p></p></div></html>

I've been using Brandon Aaron's outerHtml plugin which looks something like this:

return $('<div>').append($("html").first().clone()).html();

However this seems to actually try to reload any externally referenced files in the document (scripts, stylesheets), which seems pretty excessive just to get the HTML source of a wrapper element. Can Javascript even put an HTML element inside a DIV element?

Locally, I get this error. Something to do with AJAX rules?

XMLHttpRequest cannot load file:///C:/demo.js?_=1311466511031. Origin null is not allowed by Access-Control-Allow-Origin.

Is there a better way to get outerHtml? I'd really like to avoid any network calls when doing this.

I'm trying to get a consistent, cross browser outerHtml for a jQuery element, particularly $("html").outerHtml(). For example if the page source is this:

<html><script src="blah.js"></script><div class="foo"   id='bar'></p></div></html>

I want to be able to use $("html").outerHtml(); to get the HTML string including the element itself, which would be:

<html><script src="blah.js"></script><div class="foo" id="bar"><p></p></div></html>

I've been using Brandon Aaron's outerHtml plugin which looks something like this:

return $('<div>').append($("html").first().clone()).html();

However this seems to actually try to reload any externally referenced files in the document (scripts, stylesheets), which seems pretty excessive just to get the HTML source of a wrapper element. Can Javascript even put an HTML element inside a DIV element?

Locally, I get this error. Something to do with AJAX rules?

XMLHttpRequest cannot load file:///C:/demo.js?_=1311466511031. Origin null is not allowed by Access-Control-Allow-Origin.

Is there a better way to get outerHtml? I'd really like to avoid any network calls when doing this.

Share Improve this question edited Jul 24, 2011 at 1:02 peterjwest asked Jul 24, 2011 at 0:41 peterjwestpeterjwest 4,4522 gold badges35 silver badges49 bronze badges 4
  • Are you certain you don't want the inner HTML of body? What is your intended purpose with this data? – Conspicuous Compiler Commented Jul 24, 2011 at 0:45
  • Yes very certain. I'm validating HTML or HTML fragments. Yes I know it's not the same as the HTML source. – peterjwest Commented Jul 24, 2011 at 0:52
  • You can always do a separate HTTP request for the location of the document when looking to parse the HTML in general. However, that doesn't solve the issue for fragments. – Conspicuous Compiler Commented Jul 24, 2011 at 2:29
  • Yeah, I'm going to use both methods. – peterjwest Commented Jul 24, 2011 at 10:08
Add a ment  | 

2 Answers 2

Reset to default 4

Wrote my own solution, which simply renders the wrapping element:

(function($){
  $.fn.outerHtml = function() {
    if (this.length == 0) return false;
    var elem = this[0], name = elem.tagName.toLowerCase();
    if (elem.outerHTML) return elem.outerHTML;
    var attrs = $.map(elem.attributes, function(i) { return i.name+'="'+i.value+'"'; }); 
    return "<"+name+(attrs.length > 0 ? " "+attrs.join(" ") : "")+">"+elem.innerHTML+"</"+name+">";
  };
})(jQuery);

https://gist.github./1102076

jQuery outerHTML plugin from https://github./darlesson/jquery-outerhtml uses browser's native outerHTML when existent, as second option clone the nodes in a temporary document fragment and return its innerHTML or uses jQuery clone solution similar to Brandon Aaron's plugin.

This plugin might help you avoid the load of the reference files. In my tests in Chrome, Firefox and Internet Explorer the issue did not happen calling the code below in a page with external JavaScript files.

    var outerHTML = $(document.documentElement).outerHTML(); 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745364615a4624518.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信