PLEASE help js file does not work in localhost, when script tag in head.
document.getElementById("demo").innerHTML =
typeof "john" + "<br>" +
typeof 3.14 + "<br>" +
typeof false + "<br>" +
typeof [1,2,3,4] + "<br>" +
typeof {name:'john', age:34};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="javascript/script.js"></script>
</head>
<body>
<p id="demo"></p>
</body>
</html>
but when it in body it works.My question .why?
The problem is that your code is executed in head before the body loaded. document.getElementById("demo") won't find the element "demo" because it does not exist yet.
Use it on onload:
window.onload = function() {
document.getElementById("demo").innerHTML =
typeof "john" + "<br>" +
typeof 3.14 + "<br>" +
typeof false + "<br>" +
typeof [1,2,3,4] + "<br>" +
typeof {name:'john', age:34};
}
document.getElementById("demo") will try to find an element with id demo but since the page hasn't been rendered completely yet , it won't find anything and return null.
use window.onload.
window.onload = function(){
document.getElementById("demo").innerHTML =
typeof "john" + "<br>" +
typeof 3.14 + "<br>" +
typeof false + "<br>" +
typeof [1,2,3,4] + "<br>" +
typeof {name:'john', age:34};
};
The scripts should always be loaded at the bottom of your body, to be sure that DOM is loaded before execution. Alternatively you can use JQuery $(document).ready(function(){ //put your code in here });
This is because when your are putting this js snippet inside head tag it does not know what is <p id="demo"></p>
But when you put it inside body tag, first DOM get parsed then js is executed. You can still put it inside head tag by using window.load.Here is snippet
window.onload=function(){
document.getElementById("demo").innerHTML =
typeof "john" + "<br>" +
typeof 3.14 + "<br>" +
typeof false + "<br>" +
typeof [1,2,3,4] + "<br>" +
typeof {name:'john', age:34};
}
WORKING COPY
Related
For example, there is a page like below.
<html>
<head>
<title>Variables!!!</title>
<script type="text/javascript">
var lookatthis = 11;
var one = 22;
var two = 3;
var add = one + two;
var minus = one - two;
var multiply = one * two;
var divide = one/two;
document.write("First No: = " + one + "<br />Second No: = " + two + " <br />");
document.write(one + " + " + two + " = " + add + "<br/>");
document.write(one + " - " + two + " = " + minus + "<br/>");
document.write(one + " * " + two + " = " + multiply + "<br/>");
document.write(one + " / " + two + " = " + divide + "<br/>");
</script>
</head>
<body>
</body>
</html>
I want to assign the javascript variable "lookatthis" on debug console.
//apologise for my ambiguous question. I would rather say,
"I want to assign new value to variable "lookatthis" on this web-page using console on explorer."
Thank you for your kind teaching.)
Open debug console and write there:
lookatthis = 20
But this get you nothing
You can use the log method:
console.log(lookatthis);
Anywhere in your script block after your initial assignment of lookatthis, you can write the value to the console with the command:
console.log(lookatthis);
You achieve it by using prompt function
var lookatthis = prompt('Type the lokaltthis value');
If what you want is to be able to 'set' the value of lookatthis, you can use an input and using jquery or pure js get the value of the input and assign it to 'lookatthis'.
Edit: You can also use in the chrome console: lookatthis=25
but as your script loads when page loads, changes will not be shown but the value will be changed
I have a problem with JS, whenever I write "\n" while in script tag, it doesn't print new row.I am new to Javascript so can someone explain to me what is the problem?
Here is the code from my excercise:
<html>
<head>
<meta charset="utf-8">
<script>
var output;
var number = window.prompt("Enter mobile no: \n");
if( number>10000000 && number<99999999){
var ps = parseInt(number/1000000);
var vs = parseInt((number/1000)%1000);
var ts = number%1000;
output = "0" + ps + "/" + vs + "-" + ts +" ";
document.write("Mobile no. is : " + output + " " + "\n");
if( ps == 70 || ps == 71 || ps == 72 ){
document.write("Mobile no. is T-Mobile " + "\n");
}
if( ps == 75 || ps == 76){
document.write("Mobile no. is One " + "\n");
}
if( ps == 77 || ps == 78){
document.write("Mobile no. is Vip " + "\n");
}
}
</script>
</head>
<body>
//not important
</body>
</html>
You're outputting to the browser, so HTML is relevant, not plain text. Just replace the "\n" with "<br>".
You are writing HTML to the page. It does not recognize new lines. You need to use the "<br>" tag.
If you are writing in a .html file use <br> tag like this:
document.write("<br>");
Otherwise, you can use "\n".
I'm currently attempting to use the Flickr API and javascript to generate a page based on a search query, in this case, dogs. When the line
var hyperLink = "https://www.flickr.com/photos/" + photo.owner + "/" + photo.id + ".jpg";
Runs it states undefined is not an object in regards to photo.owner. I am able to print out photo.owner using console.log(photo.owner) but cannot access it to merely print out the information in the url.
Full code is:
<!DOCTYPE html>
<html lang="en">
<head>
<title>The Flickr Experiment</title>
</head>
<body id ="1">
<script>
function jsonFlickrApi(rsp) {
var str = "";
str +="<h1>Doggo piccies from Flickr</h1>";
str +="<h3>Total piccies: " + rsp.photos.photo.length;
var i;
for (i = 0; i <= rsp.photos.photo.length; i++) {
var photo = rsp.photos.photo[i];
var imageTitle = photo.title;
var hyperLink = "https://www.flickr.com/photos/" + photo.owner + "/" + photo.id + ".jpg";
var imgLink = "https://farm" + photo.farm + ".staticflickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + "_t.jpg";
str +="<img alt=\"" + imageTitle + "\" src=\"" + imgLink + "\"/>"
}
document.writeln(str);
}
</script>
<script src="https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=XXXXf&tags=golden-retriever&per_page=100&format=json">
</script>
</body>
</html>
Arrays are zero based. That means if an array's length is 3, then the highest index is 2. The loop condition must therefore be
i < rsp.photos.photo.length
//^^ strictly smaller, not smaller or equal
If not, you will be accessing an index (namely the length of the array) which doesn't exist, which will result in undefined, the error you are getting.
Simple example:
var arr = [1,2,3];
console.log(arr[0]);
console.log(arr[1]);
console.log(arr[2]);
console.log(arr[arr.length]);
This is the link to the viewable example from the book;http://beginningjs.com/examples/ch2_example7.html
This is my code saved locally; Not sure why it wont load.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title> Chapter 2, Example 7</title>
</head>
<body>
<script>
var myString = "56.02 degrees centigrade";
var myInt;
var myFloat;
document.write{"\"" + myString + "\" is + "\" + parseInt(myString, 10) +
"as an interger" + "<br/>"};
myInt = parseInt(myString, 10);
document.write("\"" + myString + "\"when converted to an interger equals " + myInt + "<br/>" );
nyFloat = parseFloat(myString);
document.write( "\"" + myString + "\" when converted to a floating point number equals " + myFloat);
document.write(myString);
</script>
</body>
</html>
I think it's loaded but your javascript syntax are wrong.
Use parentheses instead of braces on your first document.write. The script is stuck in that line and the rest of your program doesn't execute, that's why the page doesn't show anything.
Okay There is an error in your first document.write("your string") line you need to use small () braces instead of curly {} braces every time you use document.write("your string"). You have declared variable as var myFloat; and then used it as nyFloat = parseFloat(myString); which is wrong because of typo in nyFloat and (mystring,10) to print value 56.02 at the end. so the correct is myFloat = parseFloat(myString,10); There is no need to write the last line i.e. document.Write(myString); as it is printing the string again at the end. If in future you experience these kind of errors you can see these Javascript errors by using developer tool in your browser and for that you just need to press F12 from your key board as explained in one of comment under your question.
Here is the corrected code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title> Chapter 2, Example 7</title>
</head>
<body>
<script>
var myString = "56.02 degrees centigrade";
var myInt;
var myFloat;
document.write("\"" + myString + "\" is " + parseInt(myString, 10) + " as an interger" + "<br/>");
myInt = parseInt(myString, 10);
document.write("\"" + myString + "\" when converted to an interger equals " + myInt + "<br/>" );
myFloat = parseFloat(myString, 10);
document.write("\"" + myString + "\" when converted to a floating point number equals " + myFloat);
//document.write(myString);
</script>
</body>
</html>
Why doesn't this HTML/script (from "Secrets of the JavaScript Ninja") render?
http://jsfiddle.net/BCL54/
<html>
<head>
<script>
function outer(){
var a = 1;
function inner(){ /* does nothing */ }
var b = 2;
if (a == 1) {
var c = 3;
}
}
outer();
assert(true,"some descriptive text");
assert(typeof outer==='function',
"outer() is in scope");
assert(typeof inner==='function',
"inner() is in scope");
assert(typeof a==='number',
"a is in scope");
assert(typeof b==='number',
"b is in scope");
assert(typeof c==='number',
"c is in scope");
</script>
</head>
<body>
</body>
</html>
Because you didn't import Resig's necessary script containing the assert function :
<script>
function assert(pass, msg){
var type = pass ? "PASS" : "FAIL";
jQuery("#results").append("<li class='" + type + "'><b>" + type + "</b> " + msg + "</li>");
}
function error(msg){
jQuery("#results").append("<li class='ERROR'><b>ERROR</b> " + msg + "</li>");
}
function log(){
var msg = "";
for ( var i = 0; i < arguments.length; i++ ) {
msg += " " + arguments[i];
}
jQuery("#results").append("<li class='LOG'><b>LOG</b> " + msg + "</li>");
}
</script>
You can find those functions in the source of his site. Note that those function also ask for jQuery and some DOM elements into which to write. You'd better adapt to your page.
Until you're proficient enough in javascript to rewrite those functions, you'd better do the excellent exercises directly on the site.