Click here to Skip to main content
15,070,680 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<!DOCTYPE html>
<html>
<script>

    console.log("hello");
    console.log("Remembertobringanumbrella.");




</script>
<script>

    console.log("this");
</script>

<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

 this is cool
good




</body>
</html>


In my browser, this code should print hello and Remembertobringanumbrella . But, it only print this is cool good.

Any idea?
Posted

To add content to a webpage by JavaScript, you have two choices:

document.write();
append to a div or other element.

document.write will put the text wherever you call it. However, some browsers are sensitive about using document.write, so it might not always work.

JavaScript
window.onload = function() {
document.write("hello");
document.write("Remembertobringanumbrella.");
document.write("this");
}


A better idea is to append your content to a div or other element already on the page:

XML
<html>
<head><title></title></head>
<body>
<div id="myDiv"></div>
<script>
window.onload = function() {
var theElement = document.getElementById("myDiv");
var hello = document.createTextNode("hello");
var remember = document.createTextNode("Remembertobringanumbrella."); 
//'this' is a reserved word in JavaScript
//and cannot be used as a variable name
var final = document.createTextNode("this");

theElement.appendChild(hello);
theElement.appendChild(remember);
theElement.appendChild(final);
}
</script>
</body>
</html>
   
v3
Comments
   
Sure, 5ed.
—SA
You need an event to invoke your code. All JavaScript is event-driven; in other words, something has to happen for your code to execute. Such as the window loading:


JavaScript
window.onload = function() {
console.log("hello");
console.log("Remembertobringanumbrella.");
console.log("this");
}
   
v3
Comments
ankur1163 2-Jan-15 15:12pm
   
I tried running your code. It doesnt work.I dont see anything in my browser
Doug Vanderweide 2-Jan-15 15:38pm
   
You're logging to console. That's not going to appear in your web browser; it's going to show up in the console, which is some external management tool.

This will pop up three consecutive alert dialogs:

window.onload = function() {
alert("hello");
alert("Remembertobringanumbrella.");
alert("this");
}
ankur1163 2-Jan-15 15:49pm
   
alert function did bring 3 popups. What i have to do, if i want to print some message in browser?
Doug Vanderweide 2-Jan-15 15:54pm
   
See new solution below.
Use $(document).ready(function(){
Console.log("test");
})
Also give the jquery reference as $is used and on Dom ready the script will execute.
I hope this works .
Post back queries if any.
Thanks
   
v2
Comments
ankur1163 2-Jan-15 15:22pm
   
strange it doesnt work. This is the code, i am using

<!DOCTYPE html>
<html>
<script>
window.load = function() {
console.log("hello");
console.log("Remembertobringanumbrella.");
console.log("this");
}
$(document).ready(function(){
Console.log("test");
})


</script>
<script>

console.log("this");
</script>

<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>

this is cool
good




</body>
</html>
[no name] 2-Jan-15 15:24pm
   
Have you added the jquery ref??
ankur1163 2-Jan-15 15:33pm
   
Its a simple javascript code. I dont think i need jquery reference?
ankur1163 2-Jan-15 15:41pm
   
I just added prompt function in it and prompt function worked. But, console.log function doesnt work.
ankur1163 2-Jan-15 16:02pm
   
I wonder if i have to print something on page. what code i should use? I am pretty sure i dont need to use jquery for it?

I am following a book. I am doing some practice code and i have to print looping triangle. The code for this is

for (var line = "#"; line.length < 8; line += "#")
console.log(line);

But, console.log just doesnt work.
[no name] 2-Jan-15 23:46pm
   
can you do one thing, put the script at the end of the html. As we do for performance put the script after everything. So that after page load script gets loaded and executes. Please check with this. As I checked on my end its working fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900