8

Currently for a body with HTML: hello</div><p>hai</p><span>Welcome</span> on alerting $('body').html() it alerts hello<p>hai</p><span>Welcome</span>.

Fiddle

But I want it to display hello</div><p>hai</p><span>Welcome</span> i.e. Alert HTML as it is written within the body.

I can see the exact code when I view the source-code of the page.I really dont know if client-side scripts are capable of displaying the output I asked.

Is it possible ?And if yes How?

Zword
  • 6,605
  • 3
  • 27
  • 52
  • 7
    If you write invalid code, you can't expect the browser to return it to you as it will try to fix your mistakes and remove the closing tag that has no place being there. By the time javascript runs that tag has been removed, so you won't be able to get it. – adeneo Mar 13 '14 at 18:38
  • @adeneo yes I can write the correct HTML while developing an application.But my question is can I detect missing start tags – Zword Mar 13 '14 at 18:40
  • fix the div tags, why alert rather than console.log - your question does not say you want a lint checker :) – alexmac Mar 13 '14 at 18:41
  • Would `jquery.load()` return the broken divs? – NibblyPig Mar 13 '14 at 18:42
  • @Zword - Nope, the browser fixes those mistakes automatically, so the closing tag is removed from the DOM. The only way you could get it would be to get the file as text and parse it in some way yourself, but that doesn't really make much sense. – adeneo Mar 13 '14 at 18:42
  • Sorry there is no solution – Pratik Joshi Mar 13 '14 at 18:44
  • When you say "I can see the exact code when I view the source-code of the page.", are you saying the closing `` is there in the source? – gtr1971 Mar 13 '14 at 18:44
  • And by the why would you alert() the code which has missing paranthesis? – Pratik Joshi Mar 13 '14 at 18:44
  • @adeneo you are telling get the file contents and check them to detect closing tags? – Zword Mar 13 '14 at 18:45
  • @gtr1971 - Why wouldn't it be? The source and the DOM are two different things. – adeneo Mar 13 '14 at 18:45
  • 3
    @Zword - As the DOM is changed by the browser when it tries to fix mistakes in the markup, you surely can't get it from there, so the only thing I can think of is getting the content of the file with an XMLHttpRequest and parse that content yourself. – adeneo Mar 13 '14 at 18:46
  • 1
    ^^ Using $.get maybe: http://jsfiddle.net/bYWkP/show/ – A. Wolff Mar 13 '14 at 18:47
  • @A.Wolff well that can help I guess.Can you post an answer? – Zword Mar 13 '14 at 18:50
  • @Zword You may already know this but $.get == AJAX == XMLHttpRequest They are all synonyms for ajax (as are $.post and $.load also). Note that $.get, $.post and $.load are shortcuts to specific types of AJAX implementations. – cssyphus Mar 13 '14 at 18:54
  • Why don't you just validate your HTML? [W3C.org Validator](http://validator.w3.org/). – SparoHawk Mar 13 '14 at 18:54
  • possible duplicate of [Use javascript to get raw html code](http://stackoverflow.com/questions/3905219/use-javascript-to-get-raw-html-code) – Allende Mar 13 '14 at 19:20

1 Answers1

3

You could make an ajax request, this way the HTML won't be parsed:

$.get(window.location.href,function(data){console.log(data);});

See DEMO

A. Wolff
  • 74,033
  • 9
  • 94
  • 155