WT U4 Call Back Functions 3may2024
WT U4 Call Back Functions 3may2024
functions, JSON
Function Sequence:
JavaScript functions are executed in the sequence they are called. Not in the
sequence they are defined.
function myFirst() {
<!DOCTYPE html> myDisplayer("Hello");
<html> }
<body> function mySecond() {
<h2>JavaScript Function Sequence</h2> myDisplayer("Goodbye");
<p>JavaScript functions are executed in the sequence they are }
called.</p> myFirst();
<p id="demo"></p> mySecond();
<script> </script>
function myDisplayer(some) { </body>
document.getElementById("demo").innerHTML = some; </html>
}
Sample I/O:
This example will end up displaying "Hello":
function myFirst() {
<!DOCTYPE html>
myDisplayer("Hello");
<html>
}
<body>
function mySecond() {
<h2>JavaScript Function Sequence</h2>
myDisplayer("Goodbye");
<p>JavaScript functions are executed in the sequence they are
}
called.</p>
mySecond();
<p id="demo"></p>
myFirst();
<script>
</script>
function myDisplayer(some) {
</body>
document.getElementById("demo").innerHTML = some;
</html>
}
Sample I/O:
Sequence Control : Example1
Sometimes you would like to have better control over when to execute a function.
Suppose you want to do a calculation and then display the result.
You could call a calculator function (myCalculator), save the result and then call another function (myDisplayer) to display the result:
<!DOCTYPE html>
<html> function myCalculator(num1, num2) {
<body> sum = num1 + num2;
myDisplayer(sum);
<h2>JavaScript Callbacks</h2> }
The problem with the second example, is that you cannot prevent the calculator
function from displaying the result.
JavaScript Callbacks
A callback is a function passed as an argument to another function.
Using a callback, you could call the calculator function (myCalculator) with a callback and let
the calculator function run the callback after the calculation is finished:
Example:
Example:
<!DOCTYPE html> setTimeout(myFunction, 3000);
<html>
<body> function myFunction() {
document.getElementById("demo").innerHTML = “BMSCE !!";
<h2>JavaScript Callback</h2> }
</script>
<p>Wait 3 seconds (3000 milliseconds) for this page to
change.</p> </body>
</html>
<h1 id="demo"></h1> Where callbacks really shine are in asynchronous
functions, where one function has to wait for
<script> another function (like waiting for a file to load).
In the example above, myFunction is used as a callback.
<script>
Waiting for Intervals:
When using the JavaScript function setInterval(), you can specify a callback function to be executed for each interval:
Example
1000 is the number of milliseconds between intervals, so myFunction() will be called every second.
Arrow Functions
Arrow function
Example for Arrow function
Sample I/O:
Arrow functions allow us to write shorter function syntax:
<p id="demo"></p>
<script>
myFunction = (a, b) => a * b;
document.getElementById("demo").innerHTML = myFunction(4, 5);
</script>
</body>
</html>
Before: With Arrow function:
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<body> <body>
<p>This example shows the syntax of a function, without the <p>This example shows the syntax of an Arrow Function, and
use of arrow function syntax.</p> how to use it.</p>
<script> <script>
var hello; var hello;
</body> </body>
</html> </html>
Sample I/O:
Before:
hello = function() {
return "Hello World!";
}
<h2>JavaScript Function</h2>
<p id="demo"></p>
<script>
var hello;
hello = function() {
return "Hello World!";
}
document.getElementById("demo").innerHTML = hello();
</script>
</body>
</html>
With Arrow function:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var hello;
hello = () => {
return "Hello World!";
}
document.getElementById("demo").innerHTML = hello();
</script>
</body>
</html>
It gets shorter! If the function has only one statement and the statement returns a value, you can remove the brackets and the return keyword:
<!DOCTYPE html>
Sample I/O:
<html>
<body>
<p id="demo"></p>
<script>
var hello;
document.getElementById("demo").innerHTML = hello("Universe!");
</script>
</body>
</html>
In fact, if you have only one parameter, you can skip the parentheses as well:
OR
Sample I/O:
11
The JSON syntax is derived from JavaScript object notation syntax, but the JSON
format is text only. Code for reading and generating JSON data can be written in any
programming language.
JSON Example
{
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
}
<!DOCTYPE html>
<html>
Sample I/O:
<body> Ram
<p id="para"></p>
<script>
var jsonStringArray = {
// Assigned a JSON array of strings to the key "students".
"students": ["Ram", "Shyam", "Raksha",
"Akshay", "Prashant", "Varun“, ”Manju”],
};
// It returned an array. Then we accessed
// the first index of the array (which is "Ram") using [] syntax.
var x = jsonStringArray.students[0];
// Set the inner HTML of "para" paragraph to the value of variable "x".
document.getElementById("para").innerHTML = x;
</script>
</body>
</html>
Converting a JSON Text to a JavaScript Object
A common use of JSON is to read data from a web server, and display the data in a web page.
Then, use the JavaScript built-in function JSON.parse() to convert the string into a JavaScript
object:
const obj = JSON.parse(text);
Then, use the JavaScript built-in function JSON.parse() to convert the string into a JavaScript object:
const obj = JSON.parse(text);
Finally, use the new JavaScript object in your page:
use the new JavaScript object in your page:
Example:
HTTP headers play a crucial role in server and client behavior throughout the
request and response cycle. Request headers are sent by the client to the
server and contain information and instructions related to the requested
resource, while response headers are sent by the server to the client and
provide metadata, instructions, and additional information about the response
itself.
Some of most commonly used request headers are as given below:
• Accept
• User-Agent
• Authorization
• Content-type
• Cookie
User-Agent
The User-Agent header identifies the web browser or client application that is
making the request, which enables the server to tailor its response to the
client.
Authorization
The Authorization header is used to send the client’s credentials to the
server when the client is attempting to access a protected resource.
Content-Type
The Content-Type header identifies the media type of the content in the
request body.
Cookie
The client can use the Cookie header to send previously stored cookies back to the
server.
Content-Type
The Content-Type response header is the counterpart of the Content-Type request header, as it
indicates the type of data that the server is sending to the client. The header value typically
includes the media type (such as text/html, application/json, image/jpeg, and audio/mp3), as well
as any optional parameters
Cache-Control
The Cache-Control header controls caching behavior in the client’s
browser or intermediate caches. It defines how the response can be
cached, when it expires, and how it should be revalidated.
Server
The Server header includes the name and version of the server software that
generated the response, as well as information about the server’s technology
stack.
Set-Cookie
The Set-Cookie header instructs the client to store a cookie with the
specified name, value, and additional attributes, such as expiration,
domain, path, and security flags.
Content-Length
The Content-Length header, which specifies the size of the
response body in bytes, can help the client anticipate how much
data it is going to receive.
HTTP status codes
HTTP status codes are three-digit codes that indicate the outcome of
an API request. They are included in the API’s response to the API client
and they include important information that helps the client know how to
proceed.
HTTP status codes are essential to the HTTP protocol, which defines
how data is transferred between clients and servers on the internet.
Anyone who works with web-based technologies should have a solid
understanding of these codes, as they provide valuable insight that can
be used to troubleshoot issues and improve the user experience.
different types of HTTP status codes