Link to home
Start Free TrialLog in
Avatar of Divyanshu Marwah
Divyanshu Marwah

asked on

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) {
			alert(response);
			<!-- document.write("<p id=\"content\"</p>");
			for (var i = 0; i < 1; i++) {
				var item = response.items[i];
				cont.document.write(item);
				<!-- 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">
  <script>
document.write('<span style="color:red;">Hello</span>');
</script>
</div>

Open in new window

Avatar of Member_2_6317024
Member_2_6317024
Flag of United States of America image

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
document.getElemetByID()
Avatar of Rob
Where is your cont variable defined?

cont.document.write(item);

and

cont.getElementById("cont");

Do not make any sense.  Replace cont with document.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial