3

var target = '<p><img alt=\"\" src=\"upfiles\/54591303758197437.jpg\" \/><\/p>'
$(function(){
    var x=$('<div/>').text(target).html();
    alert(x);
    document.write(x)
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

I show the escaped string contained html tags and write in webpage.
The escaped string in the alert window is right, but after closing the alert window you can ony see (screenshot):
<p><img alt="" src="upfiles/54591303758197437.jpg" /></p

Where is the last > ?

Why <p><img alt="" src="upfiles/54591303758197437.jpg" /></p> won't show up in the webpage?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 2
    Why are you even using `document.write` in the first place? Even without jQuery there are far better methods of doing without what you want. – Rory McCrossan Sep 11 '18 at 12:18
  • 2
    Guys, lets give the OP some benefit of doubt. For example, this *does* work for me in Chrome and Firefox, but not in Safari. I am seeing the same error as the above in Safari on macOS. – rmn Sep 11 '18 at 12:18
  • 2
    @Xufox it's odd it works for you as I get the same result as the OP in Chrome 68: https://i.imgur.com/algJh0W.png – Rory McCrossan Sep 11 '18 at 12:24
  • makes no sense why / is escaped. – epascarello Sep 11 '18 at 12:24
  • It works fine in Firefox Nightly 64.0a1. – Sebastian Simon Sep 11 '18 at 12:25
  • @epascarello Maybe not, but that doesn't invalidate the result either. – Teemu Sep 11 '18 at 12:25
  • So simplify it to `document.write(">")` and see what happens. Betting it still happens. – epascarello Sep 11 '18 at 12:28
  • @epascarello I removed all the escaping, and changed the string to `'

    '`, it still breaks for me in Safari. Though adding an extra space at the end fixes it. :confused:
    – rmn Sep 11 '18 at 12:29
  • 2
    @epascarello `document.write(">")` doesn't print anything, as you assumed! But, `document.write("> ")` does. Haunted Safari? – rmn Sep 11 '18 at 12:31
  • 1
    Adding anything to the end fixes it. My last comment showed it happens with just that escape sequence by itself. Why? no clue, why. Why not wrap it in a span or paragraph tag? – epascarello Sep 11 '18 at 12:32
  • So, I was able to reproduce this in a different browser. This only happens when `document.write` is used _after_ the DOM is loaded (which is very bad anyway). The minimal example is `addEventListener("DOMContentLoaded", () => document.write(">"))` which prints nothing. – Sebastian Simon Sep 11 '18 at 12:42

2 Answers2

3

I had the same symptom on Chrome 69 (Windows).

Managed to fix it by adding a document.close() call, as recommended by MDN:

target = '<p><img alt=\"\" src=\"upfiles\/54591303758197437.jpg\" \/><\/p>'
$(function(){
    x=$('<div/>').text(target).html();
    alert(x);
    document.write(x);
    document.close();
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Peter B
  • 22,460
  • 5
  • 32
  • 69
0

The correct and officially documented answer to this perplexing conundrum is in @PeterB's response. For anyone looking for a quick fix, just append a space at the end of string, and you're back to sanity.

document.write(x+' ')
rmn
  • 1,119
  • 7
  • 18