I have that link http://api.openweathermap.org/data/2.5/weather?q=Andorra+la+Vella that shows on the browser a json with all the values, so I need to pick only the wind, and the temp, but I don't know how to extract it, how to do that?
I thougt in add a callback after "Andorra+la+Vella"&callback=myFunction
And develop a code on javascript to get the values that I need, but I do it and something goes wrong, that's a simple test to try this little application and then i add it to my web:
<!DOCTYPE HTML>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Test tiempo andorra</title>
<script type="text/javascript">
function showTime(obj) {
var dadIp=document.getElementById('time');
dadIp.innerHTML+=obj.sunset.wind.speed;
}
</script>
</head>
<body>
<div id="time">
<h1>Test of Time in Andorra</h1>
</div>
<script type="text/javascript" src="http://api.openweathermap.org/data/2.5/weather?q=Andorra+la+Vella&callback=showTime"></script>
</body>
</html>
Look at your JavaScript error console.
Uncaught TypeError: Cannot read property 'wind' of undefined
Then look at the JSON source.
obj.sunset doesn't exist. obj.sys.sunset does.
obj.sunset.wind doesn't exist. obj.wind does.
See a live example when the correct object paths are used.
This way for example:
var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert( obj.name === "John" )
;
In case if you need parse DateTime I advise to use moment.js library.
Related
I wanted to use pdf.js for a project of mine but I faced an issue of importing it, basically, the CDN doesn't work
Here is my code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/pdfjs-dist#2.7.570/build/pdf.min.js"></script>
</head>
<body>
<canvas id="my-canvas"></canvas>
<script>
pdfjsLib.getDocument('./ahmed.pdf').then(doc => {
console.log("this file has" + doc._pdfInfo.numPages);
});
</script>
</body>
</html>
and that is the errors that my console shows
Deprecated API usage: No "GlobalWorkerOptions.workerSrc" specified.
Uncaught TypeError: pdfjsLib.getDocument(...).then is not a function
So what should i do to solve this problem and thank you so much
You have to set GlobalWorkerOptions.workerSrc to /build/pdf.worker(.min).js of same version:
pdfjsLib.GlobalWorkerOptions.workerSrc =
"https://cdn.jsdelivr.net/npm/pdfjs-dist#2.7.570/build/pdf.worker.min.js"; 👈
pdfjsLib.getDocument('./ahmed.pdf').promise.then(doc => {
console.log(`This document has ${doc._pdfInfo.numPages} pages.");
});
And, as #Pasi has mentioned, you have to promisify .getDocument() by chaining .promise on it. Without it, there is no .then().
See the "Hello World with document load error handling" example on this page to get started: https://mozilla.github.io/pdf.js/examples/
(Your snippet is missing .promise after getDocument() and setting the workerSrc property)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
<p>Привет Мир!</p>
<script type="text/javascript">var gtElInit = function gtElInit() {var lib = new google.translate.TranslateService();lib.translatePage('ru', 'en', function () {});}</script>
<script type="text/javascript" src="https://translate.google.com/translate_a/element.js?cb=gtElInit&client=wt"></script>
</body>
</html>
Example 2
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
<p>Привет Мир!</p>
<script type="text/javascript" src="https://translate.google.com/translate_a/element.js?cb=gtElInit&client=wt"></script>
<script type="text/javascript">var gtElInit = function gtElInit() {var lib = new google.translate.TranslateService();lib.translatePage('ru', 'en', function () {});}</script>
</body>
</html>
When run this page localy from desktop in Chrome - it works (russian words after page page load translate to english). So it works well in snippet here!
But when put page to website and run like normal site from web - it DONT WORK (russian words dont translate). Webpage here: http://www.shram.kiev.ua/bak/1.shtml
Error: Uncaught TypeError: google.translate.TranslateService is not a constructor
I really dont know js but i trully need fix. Help pls. And pls give fix to my topic, because i read all topics about "is not a constructor" but dont understand :(
You need to load the translate library before you try to use it. On an HTML page, <script> tags are loaded in the order they appear, so you need to move the script tag loading Google translate above the script tag where you are trying to use it.
image from html codethis is the screenshot of the consolestrong text
2 images attached
When i am trying to fetch the id its saying Undefined. i am trying like response[0].id . if we write like response[0] it shows json object also.
Also if we copy the json data to console and paste the value of response to a variable and then do response[0].id then it works. but from code it doesnot.
Seems a problem with your JSON data.
This is the code i have written working fine with javascript.
<!doctype html>
<html>
<head >
<title>
stack
</title>
<meta charset="utf-8">
</head>
<body>
<button onclick="myFunction()">Click me</button>
</body>
<script>
var response=[{"id":"1", "name":"abhishek","gender":"M"},{"id":"1", "name":"abhishek","gender":"M"}];
function myFunction() {
console.log(response[0].id);
}
</script>
</html>
Hello can you please try something like:
var listdata = [{"id":"1", "name":"abhishek","gender":"M"},{"id":"2", "name":"yogesh","gender":"M"},{"id":"3", "name":"rubi","gender":"F"}];
console.dir(listdata[0].id);
alert(listdata[0].id);
you can see running example here
var lPT = document.title.split(' -')[0];
$('.pageBar .left').text(lPT);
With above code snippet, I am trying to display the title of webpage as in below example,
'Home - Sample Website' would come out as 'Home'
The html would be,
<div class="pageBar"><div class="left"></div></div>
I am trying this, but it is not displaying the title. I am relatively new to JS, and I would like to know, what am I doing wrong?
Try using this,
var lPT = document.title.split('-')[0].trim();
$('.pageBar .left').text(lPT);
Also, make use that you are not getting errors in the browser console and you have included the jquery file before this snippet.
Make sure that page title contains - else it will not show anything.
Check below example:
var lPT = document.title.split(' -')[0];
console.log(lPT);
$('.pageBar .left').text(lPT);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<title>Sarjan - My page</title>
<div class="pageBar"><div class="left"></div></div>
Just like T.J.Crowder said:
you need to import jquery
you need to place your code after the you want to manipulate with that code (otherwise when your code runs the DOM element with the .pageBar does not exists yet )
Here is an example:
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home - Sample Website</title>
</head>
<body>
<div class="pageBar">
<div class="left">
Sample text
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
var lPT = document.title.split(' -')[0];
$('.pageBar .left').text(lPT);
</script>
</body>
</html>
I've an html page with JavaScript. Inside it I load another JavaScript with the tag :
<script type="text/javascript" charset="utf-8" src="/../myfile.js" ></script>
Furthermore my html page is encoded with the following tag :
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Then, inside a function I get a string defined in "myfile.js" and want to compare it to a string coming from my main page. I tried indexOf, ==, search, match... but the assertion will always be false. I alerted my strings and they were showed as equal (case sensitive).
Anybody has an idea of why my test doesn't work?
Edit : my code looks like the following.
window.lang= new myobject(); // this object is defined in myfile.js
var mystring1 = window.lang.attr1['mykey'];
var mystring2 = $("#mydivid").html();
alert(mystring1+":"+mystring2); // this shows 2 equal strings
/* Here I wanted to test if the 2 strings are equals and tried "==", mystring1.indexOf(mystring2), match, search, ... */
And in file.js :
myobject.prototype.attr1 = {
'mykey': 'mystring1value'
}
You need "utf-8" ,not "uft-8":
<script type="text/javascript" charset="utf-8" src="/../myfile.js" ></script>
Sample1.html
New Document
</HEAD>
<script type="text/javascript" charset="utf-8" src="Sample2.js" ></script>
<script>
function compare(){
j="test";
alert(i==j);
}
</script>
<BODY onload="compare();">
</BODY>
</HTML>
Sample2.js
var i="test";
Please try this its works fine for me
Finaly the issue came from a jquery plugin (jsTree) that added some "\n" or "\r" at the end of my strings. Sorry for disturbance and thanks for help !
Cheers,
Ricola3D