Introduction :
URL shortener is a web-based tool that allows users to convert long URLs into shortened versions. This is particularly useful for sharing links where character space is limited or for simplifying complex URLs for easier recall.
In the digital age, where information is abundant and attention spans are short, the ability to communicate effectively and efficiently is paramount. This is where this URL shortener application comes into play. It serves as a digital tool that addresses the need for concise communication, especially in the realm of web addresses.
A URL shortener is a web service that converts long URLs into manageable, shortened links. These shortened links are easier to share, especially on platforms where character count is limited, such as Twitter. They also make for cleaner, more aesthetically pleasing presentations, as they don’t overwhelm the viewer with lengthy strings of characters.
This application is not just about trimming down URLs; it’s about enhancing user experience and facilitating smoother online interactions. By providing a quick and simple way to shorten links, users can focus more on the content they’re sharing rather than the unwieldy URLs that accompany it.
The application is built using the foundational web technologies: HTML, CSS, and JavaScript. HTML lays the groundwork, creating the structure and defining the content of the web page. CSS steps in to style this content, giving it form and personality. JavaScript then breathes life into the page, making it interactive and capable of performing tasks—in this case, shortening URLs.
At its core, the application embodies the principles of web development, combining structure, style, and functionality to solve a real-world problem. It’s a testament to the power of code and its ability to create tools that are not only useful but also intuitive and accessible to a wide audience.
In essence, your URL shortener application is a bridge between the complex, often chaotic world of web addresses and the simplicity that users crave. It’s a tool that empowers users to communicate more effectively, reinforcing the idea that in our fast-paced, hyper-connected world, simplicity is key.
Explanation :
Our application is a web-based tool that allows users to convert long URLs into shortened versions. This is particularly useful for sharing links where character space is limited or for simplifying complex URLs for easier recall.
HTML Structure
The HTML structure is straightforward. It includes:
- A
DOCTYPE
declaration to define the document type and version of HTML. - The
html
element with alang
attribute specifying the language as English. - A
head
section containing meta tags for character set and viewport settings, a title, and a link to the CSS stylesheet. - A
body
section with adiv
container that includes:- A label and paragraph providing instructions to the user.
- An input field for users to enter the URL they wish to shorten.
- A button that triggers the
shortURL()
function when clicked. - A
div
where the result (shortened URL) will be displayed.
CSS Styling
The CSS defines the visual appearance of the application:
- A reset rule for margin, padding, and box-sizing.
- A font-family set globally.
- The
body
styled to center the content and set a background color. - The
.container
styled as a flex container to organize its children vertically. - Styling for the label, input field, button, and result display to make them visually appealing.
- Media queries to adjust the layout responsively for smaller screens.
JavaScript Logic
The JavaScript provides the core functionality of the application:
- The
shortURL()
function is an asynchronous function that:- Retrieves the user’s input from the input field.
- Sends a request to the
tinyurl.com
API to create a shortened URL. - If the response is successful, it displays the shortened URL as a clickable link.
- If there’s an error, it displays an error message.
Purpose of Functions
shortURL()
: This is the main function that handles the URL shortening process. It uses modern JavaScript features likeasync/await
for asynchronous operations and template literals for string interpolation.
Explanation in Detail
When a user enters a URL and clicks the “Short Url” button, the shortURL()
function is called. This function:
- Grabs the value from the input field.
- Constructs a request to the
tinyurl.com
API with the entered URL. - Waits for the response using the
await
keyword. - Checks if the response is OK (status code 200-299).
- If successful, it converts the response to text and updates the
innerHTML
of theresult
div with the shortened URL. - If not successful, it displays an error message.
This application is a great example of how HTML, CSS, and JavaScript work together to create a functional and user-friendly web tool. The HTML provides the structure, the CSS adds style, and the JavaScript adds interactivity and connects to external services to perform the URL shortening task. The use of modern JavaScript features like fetch
for network requests and async/await
for handling asynchronous code makes the code clean and readable. The application also demonstrates good practices like responsive design for different screen sizes and user feedback on operation success or failure. Well done on putting together a neat project! 👍
SOURCE CODE:
HTML (index.html)
Url-shortner
Generate a short url and redirect to
the
when a user click on the short Url
CSS (Style.css)
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background-color: #ffffff;
display: flex;
justify-content: center;
align-items: center;
padding-top: 8rem;
}
.container {
display: flex;
flex-direction: column;
padding: 40px;
justify-content: center;
align-items: center;
gap: 10px;
width: 60vw;
}
label {
font-size: 2rem;
padding: 5px;
color: rgb(96, 49, 49);
}
#url {
padding: 10px;
border-radius: 30px;
border: none;
width: 60%;
background-color: #dadada;
}
#url::placeholder {
text-align: center;
font-size: 14px;
}
button {
padding: 10px;
width: 30%;
border-radius: 50px;
border: none;
background-color: #3ec24b;
color: white;
}
#result {
padding-top: 3rem;
display: block;
flex-direction: column;
color: rgb(0, 0, 0);
}
p {
color: rgb(0, 0, 0);
}
/*-------------Media---querie----------------------*/
@media screen and (max-width: 500px) {
label {
font-size: 1rem;
}
#url {
width: 100%;
}
button {
width: 60%;
}
}
@media screen and (max-width: 370px) {
label {
font-size: 1rem;
}
#url {
width: 100%;
}
button {
width: 60%;
}
}
JavaScript (index.js)
async function shortURL() {
const Url = document.getElementById("url").value;
const response = await fetch(`https://tinyurl.com/api-create.php?url=${encodeURIComponent(Url)}`);
if (response.ok) {
const data = await response.text();
document.getElementById('result').innerHTML = `
shortend URL : ${data}`;
}
else {
document.getElementById('result').innerHTML = "Error shortening"
}
}