0

I tried to redefine the document.ready function to capture what should have been written after document.ready

$(document).ready(function(){
 document.write = function(html){
    //... do something with HTML
 };
});

I do have the HTML now which should be rendered, but I don't know where it should belong to.

Is there a way to find out which script called the document.write function (in order to place the HTML code in the right place)?

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Simon Ferndriger
  • 4,455
  • 6
  • 28
  • 53
  • Maybe I'm missing something here, but are you trying to override the `document.write` method? – emerson.marini Jan 08 '15 at 16:00
  • possible duplicate of [JavaScript - controlling the insertion point for document.write](http://stackoverflow.com/questions/1536970/javascript-controlling-the-insertion-point-for-document-write) – emerson.marini Jan 08 '15 at 16:01
  • redefine it outside of the ready event, and before any other scripts that might use it are loaded – charlietfl Jan 08 '15 at 16:07
  • 1
    `in order to place HTML code in the right place` - if you're trying to find out which script is doing such a thing than I'm 99% certain that they don't use document.write (because document.write will delete the page and insert the HTML into a blank page instead of placing it "in the right place"). Google about the DOM and innerHTML – slebetman Jan 08 '15 at 16:12
  • @slebetman it is possible though if you consider a widget script that requires it to be placed in location within html that output is expected, but then you try to use alternate approach to asynchronously load that script such as ajax loading a template that includes the widget – charlietfl Jan 08 '15 at 16:18
  • @MelanciaUK Yes, because after the document is ready, document.write does not work anymore by default – Simon Ferndriger Jan 09 '15 at 16:03

1 Answers1

1

You don't need to use document.write in this situation. You use a jquery selector and the .html() method (or .append() or similar) to place content inside an element.

For example, if you had a div with the class container

var content = '<p>Some content</p>';
$('div.container').html(content);

this code would replace the contents of the container with the value of content. You'll notice that CSS style selectors are used for selecting elements, so to select the entire body you can use

$('body')

or for all text inputs, you can use

$('input[type="text"]')

.html() will replace the entire innner content, and .append() will add to the element after the other content.

worldofjr
  • 3,868
  • 8
  • 37
  • 49