1+ <!DOCTYPE html>
2+ < html lang ="en ">
13< head >
4+ < meta charset ="UTF-8 ">
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6+ < title > URL Shortener</ title >
7+ < style >
8+ /* Reset some default styles */
9+ body , h1 , form , input , button , div {
10+ margin : 0 ;
11+ padding : 0 ;
12+ box-sizing : border-box;
13+ }
214
15+ body {
16+ font-family : 'Arial' , sans-serif;
17+ background-color : # f4f4f9 ;
18+ color : # 333 ;
19+ display : flex;
20+ justify-content : center;
21+ align-items : center;
22+ min-height : 100vh ;
23+ }
24+
25+ h1 {
26+ text-align : center;
27+ color : # 4CAF50 ;
28+ font-size : 2rem ;
29+ margin-bottom : 20px ;
30+ }
31+
32+ form {
33+ background : # fff ;
34+ padding : 20px ;
35+ border-radius : 8px ;
36+ box-shadow : 0 4px 10px rgba (0 , 0 , 0 , 0.1 );
37+ display : flex;
38+ flex-direction : column;
39+ align-items : center;
40+ width : 100% ;
41+ max-width : 400px ;
42+ }
43+
44+ input [type = "text" ] {
45+ padding : 12px 15px ;
46+ width : 100% ;
47+ border : 1px solid # ddd ;
48+ border-radius : 6px ;
49+ margin-bottom : 10px ;
50+ font-size : 1rem ;
51+ }
52+
53+ button {
54+ padding : 12px 20px ;
55+ background-color : # 4CAF50 ;
56+ color : # fff ;
57+ border : none;
58+ border-radius : 6px ;
59+ cursor : pointer;
60+ font-size : 1rem ;
61+ width : 100% ;
62+ transition : background-color 0.3s ease;
63+ }
64+
65+ button : hover {
66+ background-color : # 45a049 ;
67+ }
68+
69+ # result {
70+ margin-top : 20px ;
71+ text-align : center;
72+ }
73+
74+ # result a {
75+ color : # 4CAF50 ;
76+ text-decoration : none;
77+ font-weight : bold;
78+ }
79+
80+ # result a : hover {
81+ text-decoration : underline;
82+ }
83+
84+ /* Responsive styling */
85+ @media (max-width : 480px ) {
86+ h1 {
87+ font-size : 1.5rem ;
88+ }
89+
90+ input [type = "text" ], button {
91+ font-size : 0.9rem ;
92+ }
93+ }
94+ </ style >
95+ < script >
96+ document . addEventListener ( 'DOMContentLoaded' , function ( ) {
97+ const form = document . getElementById ( 'shortenForm' ) ;
98+ const resultDiv = document . getElementById ( 'result' ) ;
99+
100+ if ( form ) {
101+ form . addEventListener ( 'submit' , async function ( event ) {
102+ event . preventDefault ( ) ;
103+ const url = document . getElementById ( 'urlInput' ) . value ;
104+
105+ const response = await fetch ( '/shorten' , {
106+ method : 'POST' ,
107+ headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } ,
108+ body : `url=${ encodeURIComponent ( url ) } `
109+ } ) ;
110+
111+ // Insert HTML response and execute script
112+ const htmlContent = await response . text ( ) ;
113+ resultDiv . innerHTML = htmlContent ;
114+
115+ // Manually execute script
116+ const script = resultDiv . querySelector ( 'script' ) ;
117+ if ( script ) {
118+ eval ( script . innerText )
119+ }
120+ } ) ;
121+ }
122+ } ) ;
123+ </ script >
3124</ head >
4125< body >
5- < h1 > URL Shortener</ h1 >
6- </ body >
126+ < main >
127+ < h1 > URL Shortener</ h1 >
128+ < form id ="shortenForm " action ="/shorten " method ="POST ">
129+ < input type ="text " name ="url " id ="urlInput " placeholder ="Enter URL to shorten " required >
130+ < button type ="submit "> Shorten</ button >
131+ </ form >
132+ < div id ="result "> </ div >
133+ </ main >
134+ </ body >
135+ </ html >
0 commit comments