Skip to content

Commit c706e06

Browse files
Add Web APIs section with examples (#213)
* Add Web APIs section with examples * Update web-apis.md * Update web-apis.md * Corrected markdown syntax and added more context to Fetch API section * Update web-apis.md fix merge conflicts --------- Co-authored-by: Suman Kunwar <[email protected]>
1 parent cae2703 commit c706e06

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

web-apis.md

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Web APIs
2+
3+
Web APIs are interfaces provided by the browser that allow you to interact with the browser and the underlying operating system.
4+
5+
Some common Web APIs include:
6+
7+
- **DOM API**: Allows for dynamic manipulation of HTML and CSS.
8+
- **Fetch API**: Enables network requests to retrieve resources from servers.
9+
- **Geolocation API**: Provides the user's location.
10+
- **Canvas API**: Used for drawing graphics via JavaScript.
11+
- **Storage API**: Allows for storing data on the client-side.
12+
13+
### Why Use Web APIs?
14+
15+
Web APIs are essential for modern web development because they:
16+
- **Enable Rich User Experiences**: They allow developers to create interactive and dynamic web applications.
17+
- **Provide Access to Browser Capabilities**: Web APIs offer access to features like geolocation, notifications, and local storage.
18+
- **Facilitate Communication with Servers**: They make it easy to fetch data from servers, submit forms, and interact with web services.
19+
20+
### How Web APIs Work?
21+
22+
Web APIs work by providing a set of functions and properties that developers can use to interact with the browser. These functions are exposed by the browser and can be called using JavaScript. When a function is called, the browser performs the corresponding action, such as fetching data from a server or updating the DOM.
23+
24+
#### Example Workflow
25+
26+
1.User Action: The user interacts with the web application, such as clicking a button.
27+
28+
2.API Call: JavaScript code in the web application makes a call to a Web API.
29+
30+
3.Browser Action: The browser performs the requested action, such as retrieving data or updating the display.
31+
32+
4.Response Handling: The web application handles the response from the API and updates the user interface accordingly.
33+
34+
35+
## DOM Manipulation
36+
37+
The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content.
38+
39+
### Example Code: Changing the Content of an Element
40+
41+
42+
```javascript
43+
document.getElementById("demo").innerHTML = "Hello, World!";
44+
```
45+
46+
## Fetch API
47+
48+
49+
The Fetch API provides an interface for fetching resources (including across the network).
50+
51+
### Example Code: Simple Fetch Request
52+
53+
54+
```javascript
55+
fetch('https://api.example.com/data')
56+
.then(response => response.json())
57+
.then(data => console.log(data))
58+
.catch(error => console.error('Error:'));
59+
```
60+
61+
## Geolocation API
62+
63+
The Geolocation API allows the user to provide their location to web applications if they so desire. For privacy reasons, the user is asked for permission to report location information.
64+
65+
### Example Code: Getting User Location
66+
67+
```javascript
68+
if ("geolocation" in navigator) {
69+
navigator.geolocation.getCurrentPosition(function(position) {
70+
console.log("Latitude: " + position.coords.latitude);
71+
console.log("Longitude: " + position.coords.longitude);
72+
});
73+
} else {
74+
console.log("Geolocation is not supported by this browser.");
75+
}
76+
```
77+
## Canvas API
78+
79+
The Canvas API provides a means for drawing graphics via JavaScript and the HTML <canvas> element.
80+
81+
###Example Code: Drawing Shapes on Canvas
82+
83+
```javascript
84+
let canvas = document.getElementById('myCanvas');
85+
let context = canvas.getContext('2d');
86+
87+
// Draw a rectangle
88+
context.fillStyle = '#FF0000';
89+
context.fillRect(10, 10, 150, 100);
90+
```
91+
92+
## Storage API
93+
94+
The Web Storage API provides mechanisms by which browsers can store key-value pairs, in a much more intuitive fashion than using cookies.
95+
96+
### Example Code: Storing Data in localStorage
97+
98+
99+
#### Local stoage
100+
```javascript
101+
// Save data to local storage
102+
localStorage.setItem('key', 'value');
103+
104+
// Retrieve data from local storage
105+
let value = localStorage.getItem('key');
106+
console.log(value);
107+
```
108+
#### Session Storage
109+
110+
```javascript
111+
// Save data to session storage
112+
sessionStorage.setItem('key', 'value');
113+
114+
// Retrieve data from session storage
115+
let value = sessionStorage.getItem('key');
116+
console.log(value);
117+
```
118+
119+
## Notification API
120+
121+
The Notification API allows web pages to control the display of system notifications to the end user.
122+
123+
### Example Code: Displaying a Notification
124+
125+
```javascript
126+
127+
if (Notification.permission === "granted") {
128+
new Notification("Hello, World!");
129+
} else if (Notification.permission !== "denied") {
130+
Notification.requestPermission().then(permission => {
131+
if (permission === "granted") {
132+
new Notification("Hello, World!");
133+
}
134+
});
135+
}
136+
```

0 commit comments

Comments
 (0)