Javascript Error

Hi I am using the following javascript function in my code to receive the json response. But I want to display the result in the cont div tag, but document.write and document.getElementbyId is not working. When I use document.write the error being displayed is :
Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
And if I am using cont.write following error is shown:
Uncaught TypeError: cont.write is not a function
function hndlr(response) {
			<!-- document.write("<p id=\"content\"</p>");
			for (var i = 0; i < 1; i++) {
				var item = response.items[i];
				<!-- document.write('poi');
		// in production code, item.htmlTitle should have the HTML entities escaped.
        cont.getElementById("cont").innerHTML =cont.getElementById("cont").innerHTML+ "<br>" + item.htmlTitle;

<div id="cont" style="height: auto; width: 950px">
document.write('<span style="color:red;">Hello</span>');

Open in new window

Divyanshu MarwahAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jessica WhitleySoftware DeveloperCommented:
With the snippet included I need to ask do you have an @includes statement for the file in question?
If not that would solve the problem you are experiencing. There is no way to reference the file if they are not in fact linked somehow.

To get elements by Id
RobOwner (Aidellio)Commented:
Where is your cont variable defined?




Do not make any sense.  Replace cont with document.
Julian HansenCommented:
As the error says document.write is not possible after the page has loaded.

What you need to do is
a) Get the element (cont) you want to send the JSON results to
b) Construct the elements you want to appear in the div
c) Use innerHTML to set the contents.

The question is - do you have control over the JSON that you are returning i.e. could you return the markup you want in the result div - or are you forced to receive the JSON?

Two examples outlining each of the above
HTML Returned
function hndlr(response) {
   document.getElementById('cont').innerHTML = response;

Open in new window

JSON Returned
function hndlr(response) {
  var el = document.getElementById('cont');
   for(var i = 0; i < response.items.length;i++) {
      var new_el = document.createElement('span');
      new_el.innerHTML = response.items[i];

Open in new window

You would need to post more information about your returned JSON in order to provide a complete solution - or you can adapt the above to do what you need.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.