Get Base URL

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the fundamentals of JavaScript programming language. You will learn how to write basic syntax, manipulate data types, use conditional statements, loops, and functions. By the end of this lab, you will have a solid foundation in JavaScript and be able to create simple programs.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") subgraph Lab Skills javascript/variables -.-> lab-28351{{"`Get Base URL`"}} javascript/data_types -.-> lab-28351{{"`Get Base URL`"}} javascript/arith_ops -.-> lab-28351{{"`Get Base URL`"}} javascript/comp_ops -.-> lab-28351{{"`Get Base URL`"}} end

Retrieving Base URL

To retrieve the base URL from a given URL, follow these steps:

  1. Open the Terminal/SSH.
  2. Type node to start practicing coding.
  3. Use the following JavaScript function to get the current URL without any parameters or fragment identifiers:
const getBaseURL = (url) => url.replace(/[?#].*$/, "");
  1. Replace url with the URL you want to retrieve the base URL from.
  2. The function will remove everything after either '?' or '#', if found, and return the base URL.
  3. Here's an example:
getBaseURL("http://url.com/page?name=Adam&surname=Smith");
// 'http://url.com/page'

Summary

Congratulations! You have completed the Get Base URL lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like