How to Dynamically Generate Website URL in JavaScript

How to Dynamically Generate Website URL in JavaScript

Introduction

In this tutorial we will create a How to Dynamically Generate Website URL in JavaScript. This tutorial purpose is to teach you how to dynamically generate URL. This will thoroughly show you the simple way generating URL . I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that create a URL link. I will give my best to provide you the easiest way of creating this program Generate Website URL. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html form and buttons. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4.         <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5.         <nav class="navbar navbar-default">
  6.                 <div class="container-fluid">
  7.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  8.                 </div>
  9.         </nav>
  10.         <div class="col-md-3"></div>
  11.         <div class="col-md-6 well">
  12.                 <h3 class="text-primary">How to Dynamically Generate Website URL</h3>
  13.                 <hr style="border-top:1px dotted #ccc;"/>
  14.                 <div class="col-md-12">
  15.                         <div class="form-inline ">
  16.                                 <input type="text"  placeholder="Enter Website name" id="website" class="form-control"/> <input type="text" placeholder="Enter URL" id="link" class="form-control"/> <button type="button" onclick="generateURL();" class="btn btn-primary">Generate URL</button>
  17.                                 <h5>Example URL's:</h5>
  18.                                 <h6 class="text-primary">google.com, youtube.com, facebook.com, etc...</h6>
  19.                         </div>
  20.                 </div>
  21.                 <div class="col-md-8">
  22.                 <h4>WEBSITE URL Links</h4>
  23.                 <ul id="result"></ul>
  24.                 </div>
  25.         </div>
  26. <script src="script.js"></script>
  27. </body>
  28. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will allow you to dynamically generate URL. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function generateURL(){
  2.                 var website = document.getElementById('website').value;
  3.                 var link = document.getElementById('link').value;
  4.                 if(website == "" || link == ""){
  5.                         alert("Please enter something first");
  6.                 }else{
  7.                         var a=document.createElement('a');
  8.                         var list=document.createElement('li');
  9.                        
  10.                         a.textContent=website;
  11.                         a.setAttribute("href", "https://"+link);
  12.                         a.setAttribute("target", "_blank");
  13.                         list.appendChild(a);
  14.  
  15.                         document.getElementById('result').appendChild(list);
  16.                         document.getElementById('website').value=""
  17.                         document.getElementById('link').value=""
  18.                 }
  19. }

In the code above we just create one method called generateURL(), this function will dynamically generate your website URL. The code is consist of several appending of data including the URL that we submit in the form. We just append the URL data that we entered and send it to HTML list, by creating new element that we will be set as a list.

Output:

The How to Dynamically Generate Website URL in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Dynamically Generate Website URL in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment