Client-side Javascript can appear where within an HTML document? - javascript

Client-side Javascript can appear where within an HTML document?
A. Between the <head> and </head> tags
B. Between the <body>and </body> tags
C. Both of the above
D. None of the above

Here are codes that are placed in the different parts of the HTML. Ever variation will run no matter where the script tag is placed.
1.
<html>
<head>
<title>Foo</title>
<script type="text/javascript">
alert("hello world");
</script>
</head>
<body>
</body>
</html>
2.
<html>
<head>
<title>Foo</title>
</head>
<body>
<script type="text/javascript">
alert("hello world");
</script>
</body>
</html>
3.
<html>
<head>
<title>Foo</title>
</head>
<body>
</body>
<script type="text/javascript">
alert("hello world");
</script>
</html>
If you try out all these 3 variations where the script tags are placed in different parts of the HTML, you'll see that the alert() call will still run; regardless of their position in the HTML document.
So I think the answer to your question is:
C. Both of the above
Since there is no choice that says "Anywhere within the html document"

It is a good idea to place scripts at the bottom of the element.
This can improve page load, because HTML display is not blocked by scripts loading.
You should prefer here:

I chose "C" both of the above because the "Script" can be placed anywhere between the and < / body> as well as the < head> and < / head> It appears the most logical answer to go with in this situation is "C"

Related

External JavaScript File not working when included in body of HTML page [duplicate]

I'm trying to use an external JavaScript file in order to write "Hello World" into a HTML page.
However for some reason it does not work, I tried the same function and commands inline and it worked, but not when it's using an external JavaScript file. The part I commented out in the JS file was the previous method I was trying to use. Those lines of could worked when I ran the script from the header, and inline. Thanks
Html file:
<html>
<head>
</head>
<body>
<p id="external">
<script type="text/javascript" src="hello.js">
externalFunction();
</script>
</p>
<script type="txt/javascript" src="hello.js"></script>
</body>
</html>
JavaScript file
function externalFunction()
{
var t2 = document.getElementById("external");
t2.innerHTML = "Hello World!!!"
/*document.getElementById("external").innerHTML =
"Hello World!!!";*/
}
In general, you want to place your JavaScript at the bottom of the page because it will normally reduce the display time of your page. You can find libraries imported in the header sometimes, but either way you need to declare your functions before you use them.
http://www.w3schools.com/js/js_whereto.asp
index.html
<!DOCTYPE html>
<html>
<head>
<!-- You could put this here and it would still work -->
<!-- But it is good practice to put it at the bottom -->
<!--<script src="hello.js"></script>-->
</head>
<body>
<p id="external">Hi</p>
<!-- This first -->
<script src="hello.js"></script>
<!-- Then you can call it -->
<script type="text/javascript">
externalFunction();
</script>
</body>
</html>
hello.js
function externalFunction() {
document.getElementById("external").innerHTML = "Hello World!!!";
}
Plunker here.
Hope this helps.
Script tags with SRC values do not run the contents. Split it to two script tags. One for the include, one for the function call. And make sure the include is before the call.
use onload eventListener to make it simple
<script>
window.onload = function() {
externalFunction();
}
</script>
You're trying to call the function before it has been loaded.
Place the load script above the declaration:
<html>
<head>
<script type="txt/javascript" src="hello.js"></script>
</head>
<body>
<p id="external">
<script type="text/javascript">
externalFunction();
</script>
</p>
</body>
</html>
Also you have a typo:
<script type="txt/javascript" src="hello.js"></script>
Should be:
<script type="text/javascript" src="hello.js"></script>
The script type needs to be "text/javascript" not "txt/javascript".

Javascript and print on the page

I started to learn javascript and I saw the following code in one of Javascript books:
<!DOCTYPE html>
<html>
<head>
<title>A Simple JavaScript Example</title>
</head>
<body>
<p>Printed before JavaScript is run.</p>
<p>
<script type=”text/javascript”>
document.write(“Printed by JavaScript.”);
</script>
</p>
<p>Printed after JavaScript is run.</p>
</body>
</html>
According to the book, three lines has to be printed. However, when I open this html file in the browser I see only the first and the last lines. The line "Printed by JavaScript" doesn't show.
Do you have any idea what can be the problem?
I guess it's because you used incorrect double quotes (wrong copy-paste from the book sample):
”
should become:
"
so that you have:
<script type="text/javascript">
document.write("Printed by JavaScript.");
</script>

my console.log does not work in my html page

I am very new to javascript but I can't go forward in learning because my console.log doesn't work AT ALL I will type in the console.log message and on my html page nothing shows up. i have try to debug it but I'm so new I don't know how all I can do is go to the forums and ask, it seem some people have the problem but, there is no good answer to the problem. I've tried every thing i know how to do (which isn't very much) but nothing works PLEASE HELP!!
this is my program
<html>
<head>
<title>My First proper HTML page</title>
</head>
<body>
<h1>Hello</h1>
<p>My First web page.</p>
<script type="text/javascript">
console.log("Hello World!"</script>
</body>
</html>
i know that it has to be in dev tool to see it now but how do i make it show up in the html not in dev.?
there has to be a way!
i now now that document.write is the right way but that isn't working either
<html>
<head>
<title>My First proper HTML page</title>
</head>
<body>
<h1>Hello</h1>
<p>My First web page.</p>
<script>
var name = "Nicholas";
document.write("Hello " + name);
if (name.length > 7 {
document.write("Wow you have a Really Long name!");
}
</script>
</body>
</html>
Technically, console.log is not supposed to show up on your HTML page. Rather, it shows up on your console's (web browser) log.
Even with all the correct answers provided, you can view your answer by visiting the Developer Tool -> Console (For Chrome, on Apple, it's Option + Command + J). For Windows, using Chrome, you hold the following keys: Ctrl+Shift+J
Here is a clip of the code and the log recorded by the console:
because you have to type console.log()
not control.log
Also, console.log needs to be either inside script tags, or in a separate javascript file. In your case you would have to do:
console.log("Hello, World!");
Try the below sample
<html>
<head>
<script type="text/javascript">
console.log("Hello World")
</script>
</head>
<body>
Hello
</body>
</html>
Unless you typed/edited your post.. that has an invalid Javascript.
<html>
<head>
<title>My First proper HTML page</title>
</head>
<body>
<h1>Hello</h1>
<p>My First web page.</p>
<script type="text/javascript">
console.log("Hello World!");
</script>
</body>
</html>
You did not have a closing )
Ah, as others have explained.. Console is not your HTML page, its part of the web browser itself. For example, in google chrome press CTRL+SHIFT+J to show it.
Your after document.write

My iframe keeps displaying code injected as a string of text, not rendering

For certain reasons, I have it so a div will contain code that I want an iframe to load:
<div id="MyDiv">
<html>
<head>
</head>
<body>
<h1>Hi</h1>
</body>
</html>
</div>
<iframe id="MyFrame"></iframe>
My Javascript to get the text (the code) within the div above:
var DCode = document.getElementById("MyDiv").innerHTML;
My Jquery to add the code to my iframe:
$('#MyFrame').contents().find('html').append(DCode);
When I view the document in my iframe, I see the html from the div inside but wrapped with double quotes and in the body. How can I get it to display as a page?
One thing I realized when using an alert box to display the code, the "<" are shown as the escape characters: & l t ;
Few things to consider -
1) Contents in MyDiv - <head> and <body> tags starts but do not end.
2) var "DCode" holds innerHTML but jquery appends "DString".. Variables are different.
Is it a typo in asking the question?
I could not reproduce your issue. Following code works fine for me -
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function copy()
{
var DCode = document.getElementById("MyDiv").innerHTML;
$('#MyFrame').contents().find('html').append(DCode);
}
</script>
</head>
<body onload="copy();">
<div id="MyDiv">
<html>
<head></head>
<body>
<h1>Hi</h1>
Div body
</body>
</html>
</div>
<iframe id="MyFrame"></iframe>
</body>
</html>

HTML/Javascript Confusion

I have an extremely basic html document written as the following:
<html>
<head>
<title>A Basic Javascript Game</title>
</head>
<body>
<script language="JavaScript" src="mygame.js">
</script>
</body>
</html>
I've created an according Javascript file, have made sure that it's syntax is alright. The first line of the mygame.js file is:
var persontotalkto = prompt("You wake up one Saturday morning. The Holidays just started, and you can't wait to tell your family where you've decided for all of us to go on vacation! Who do you talk to: WIFE, SON, or DAUGHTER?").toUpperCase();
But when I open the html file, I'm not getting any prompt whatsoever. I have javascript enabled, so what is the problem?
Change the script tag to either the HTML4 version:
<script type="text/javascript" src="mygame.js"></script>
Or the HTML5
<script src="mygame.js"></script>
(Either will work on any browser released this millennium.)
Change
<script language="JavaScript" src="mygame.js">
</script>
to
<script type="text/javascript" src="mygame.js">
</script>

Categories