index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Byte Size</title> <style> header { height: 60px; text-align: center; } main { background-color: #0087de; height: 900px; text-align: center; } h1 { height: 38px; font-family: Roboto; font-size: 32px; font-weight: 300; letter-spacing: 0.6px; text-align: center; color: #1f233e; padding-top: 40px; } .logo { width: 110px; height: 28px; margin-top: 40px; object-fit: contain; } .container { width: 400px; margin: auto; text-align: center; } #input { width: 219px; height: 37px; border-radius: 3.2px; border: solid 0.8px #1f233e; background-color: inherit; display: block; margin: auto; font-family: Source Sans Pro; font-size: 14px; letter-spacing: 0.2px; text-align: center; color: #ffffff; } #shorten { width: 91px; height: 37px; border-radius: 3.2px; border: solid 0.8px #1f233e; background-color: inherit; margin-top: 48px; font-family: Source Sans Pro; font-size: 14px; letter-spacing: 0.2px; text-align: center; color: #ffffff; margin-right: -3px; } #shorten:hover { background-color: #1f233e; border: solid 0.8px #4a4a4a; color: #0087de; cursor: pointer; } #shorten:active { background-color: #ffffff; border: solid 0.8px #4a4a4a; color: #1f233e; cursor: pointer; } #responseField { min-width: 191px; min-height: 120px; border: solid 2px #ffffff; margin: auto; margin-top: 25px; font-family: Source Sans Pro; font-size: 16px; letter-spacing: 0.2px; text-align: center; color: #ffffff; overflow: auto; } </style> <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"> <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro" rel="stylesheet"> </head> <body> <header> <img src="https://s3.amazonaws.com/codecademy-content/courses/intermediate-javascript-requests/bytesize_logo.svg" class="logo" /> </header> <main id="main"> <div class="container"> <h1>Enter a URL</h1> <form id="form" autocomplete="off"> <input type="text" id="input" value=""> <button id="shorten">Shorten</button> </form> <div id="responseField"> </div> </div> </main> <script src='https://code.jquery.com/jquery-3.1.0.min.js'></script> <script> // Information to reach API const apiKey = '554b5b27161f4eadaec5de8f7c8b5a47'; const url = 'https://api.rebrandly.com/v1/links'; // Some page elements const inputField = document.querySelector('#input'); const shortenButton = document.querySelector('#shorten'); const responseField = document.querySelector('#responseField'); // AJAX functions const shortenUrl = () => { const urlToShorten = inputField.value; // We’re including this information because the API expects to see an object with a key destination that has a value of a URL. const data = JSON.stringify({destination: urlToShorten}); // new XMLHttpRequest object const xhr = new XMLHttpRequest(); xhr.responseType = 'json'; xhr.onreadystatechange = () => { if (xhr.readyState === XMLHttpRequest.DONE) { renderResponse(xhr.response); } } xhr.open('POST', url); // To access the Rebrandly API, we need a header with two key-value pairs. In the header, you must include values for 'Content-type' and an 'apikey'. xhr.setRequestHeader('Content-type', 'application/json'); xhr.setRequestHeader('apikey', apiKey); xhr.send(data); }; // Clear page and call AJAX functions const displayShortUrl = (event) => { event.preventDefault(); while(responseField.firstChild){ responseField.removeChild(responseField.firstChild); } shortenUrl(); } shortenButton.addEventListener('click', displayShortUrl); </script> <script> // Manipulates responseField to render a formatted and appropriate message const renderResponse = (res) => { // Displays either message depending on results if(res.errors){ responseField.innerHTML = "<p>Sorry, couldn't format your URL.</p><p>Try again.</p>"; } else { responseField.innerHTML = `<p>Your shortened url is: </p><p> ${res.shortUrl} </p>`; } } // Manipulates responseField to render an unformatted response const renderRawResponse = (res) => { // Displays either message depending on results if(res.errors){ responseField.innerHTML = "<p>Sorry, couldn't format your URL.</p><p>Try again.</p>"; } else { // Adds line breaks for JSON let structuredRes = JSON.stringify(res).replace(/,/g, ", \n"); structuredRes = `<pre>${structuredRes}</pre>`; responseField.innerHTML = `${structuredRes}`; } } </script> </body> </html> |