Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <head>
  <meta charset="utf-8">
  <title>Google URL Shortener</title>
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Dhurjati" rel="stylesheet">
</head>

<body>
  <main id="main">
      <h1 class="title">Link Shortener/Expander Google API</h1>
      <div class="container">

      <h1>Enter a URL</h1>

      <form id="form" autocomplete="off">
        <input type="text" id="input" placeholder="Type your link here...">
        <button id="shorten">Shorten</button>
        <button id="expand">Expand</button>
      </form>
        
      <div id="responseField"></div>
      </div>
  </main>
</body>
              
            
!

CSS

              
                body {
  padding: 0;
  margin: 0;
}

main {
  background-color: #0097de;
  height: 800px;
  text-align: center;
  border: 3px solid orange;
}

h1 {
  height: 38px;
  font-family: Roboto;
  font-size: 32px;
  font-weight: 300;
  letter-spacing: 0.6px;
  text-align: center;
  color: #1f233e;
  padding-top: 50px;
}

.title {
  font-family: 'Dhurjati', sans-serif;
  font-size: 3.4rem;
  text-decoration: underline;
  font-weight: 500;
  padding-bottom: 30px;
}

.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: #fff;
}

::placeholder {
  color: white;
}

#shorten {
  width: 91px;
  height: 37px;
  border-radius: 3.2px;
  background-color: #1f233e;
  margin-top: 48px;
  font-family: Source Sans Pro;
  font-size: 14px;
  letter-spacing: 0.2px;
  text-align: center;
  color: #ffffff;
  margin-right: 3px;
}

#expand {
  width: 91px;
  height: 37px;
  border-radius: 3.2px;
  background-color: #1f233e;
  margin-top: 48px;
  font-family: Source Sans Pro;
  font-size: 14px;
  letter-spacing: 0.2px;
  text-align: center;
  color: #ffffff;
  margin-left: -3px;
}

#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-wrap: break-word;
}


              
            
!

JS

              
                // Include data for accessing Google APIs
const apiKey = "AIzaSyBRABPG6R4FJkgtI6TrAqxnSOXoG63ip7g";
const url = "https://www.googleapis.com/urlshortener/v1/url";

// Some page elements

const $inputField = $("#input");
const $expandButton = $("#expand");
const $shortenButton = $("#shorten");
const $responseField = $("#responseField");

// AJAX functions

function expandUrl() {
  const urlToExpand = url + "?shortUrl=" + $inputField.val() + "&key=" + apiKey;
  fetch(urlToExpand)
    .then(
      response => {
        if (response.ok) {
          return response.json();
        }
        throw new Error("Request failed!");
      },
      networkError => console.log(networkError.message)
    )
    .then(jsonResponse => {
      $responseField.append(
        "<p> Your expanded URL is </p><p> " + jsonResponse.longUrl + "</p>"
      );
      return jsonResponse;
    });
}

function shortenUrl() {
  const urlWithKey = url + "?key=" + apiKey;
  const urlToShorten = $inputField.val();
  fetch(urlWithKey, {
    method: "POST",
    headers: {
      "Content-type": "application/json"
    },
    body: JSON.stringify({ longUrl: urlToShorten })
  })
    .then(
      response => {
        if (response.ok) {
          return response.json();
        }
        throw new Error("Request failed!");
      },
      networkError => console.log(networkError.message)
    )
    .then(jsonResponse => {
      $responseField.append(
        "<p> Your shortened URL is </p><p>" + jsonResponse.id + "</p>"
      );
      return jsonResponse;
    });
}

function expand() {
  $responseField.empty();
  expandUrl();
  return false;
}

function shorten() {
  $responseField.empty();
  shortenUrl();
  return false;
}

$expandButton.click(expand);
$shortenButton.click(shorten);

              
            
!
999px

Console