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

JavaScriptHTML

Avatar of undefined
Last Comment
Julian Hansen
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
Rob
Flag of Australia image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo