How can I extract a substring from a URL and then save it to the clipboard?

Hi there! I'm working on a project for my school and could really use your expertise.

I need help extracting a substring from a URL, like this one: URL = https://www.example.com/blah/blah&code=12432 The substring is: 12432 I also want to display the substring in a Copy Box.

Your assistance with this matter would be greatly appreciated. It's crucial for my project to be able to extract text from a string.

(function() {
  var copyButton = document.querySelector('.copy button');
  var copyInput = document.querySelector('.copy input');
  copyButton.addEventListener('click', function(e) {
    e.preventDefault();
    var text = copyInput.select();
    document.execCommand('copy');
  });

  copyInput.addEventListener('click', function() {
    this.select();
  });
})();







   html, body {
          height: 100%;
        }
    
        body {
          font-size: 16px;
          background: #FFD1DD;
          display: flex;
          align-items: center;
          justify-content: center;
        }
    
        * {
          box-sizing: border-box;
        }
    
        .wrapper {
          padding: 0 5px;
        }
    
        h1 {
          text-align: center;
          font-size: 40px;
          margin-bottom: 1.2em;
          text-decoration: underline;
          text-transform: uppercase;
        }
    
        p {
          font-family: 'VT323', monospace;
          font-size: 20px;
        }
    
        .container {
          display: flex;
          background: #FFA3BB;
          border-radius: 7px;
          padding: 10px;
          margin: 0 auto;
        }
    
        h3 {
          font-size: 28px;
          text-transform: uppercase;
          text-align: center;
          
          span {
            display: inline-block;
            position: relative;
          }
        }
    
        .copy, .paste {
          flex-grow: 1;
          width: 50%;
        }
    
        .copy {
          border-right: 2px solid;
          padding-right: 10px;
          
          h3 {
            span {
              background: #76ECFF;
            }
          }
          
          input {
            padding-right: 90px;
          }
        }
    
        .paste {
          padding-left: 10px;
          
          h3 {
            span {
              background: #FAE916;
            }
          }
        }
    
        form {
          position: relative;
          width: 100%;
          
          input {
            display: block;  
            width: 100%;
            border: 3px solid;
            outline: 0;
            background: #FFF;
            font-size: 25px;
            padding: 5px 4px;
            margin-bottom: 20px;
          }
          
          button {
            display: block;
            position: absolute;
            top: 50%;
            right: 10px;
            transform: translateY(-50%);
            border: 0;
            outline: 0;
            color: #FFF;
            background: #000;
            font-family: 'VT323', monospace;
            font-size: 25px;
            text-transform: uppercase;
            padding: 0.08em 0.8em;
            cursor: pointer;
          }
        }

<div class="wrapper">
  <h1>Link Copy</h1>
  
  <p>Select the link text by clicking within the input then copy yourself or just click the copy button.  Paste into the paste side to see that it works!</p>
  <div class="container">
    <div class="copy">
       <h3>Copy <span><i class="fa fa-hand-peace-o"></i></span></h3>
      <form>
        <input type="text" value="https://codepen.io/she_codes/pen/OgrJJe/">
        <button type="button">Copy</button>
      </form>
    </div>
    <div class="paste">
      <h3>Paste <span><i class="fa fa-smile-o"></i></span></h3>
      <form>
        <input type="text">
      </form>
    </div>
  </div><!-- end .container -->
</div><!-- end .wrapper -->

Answer №1

Give this a try, it should do the trick

const query = new Proxy(new URLSearchParams(window.location.search), {
      get: (searchParams, prop) => searchParams.get(prop),
    });
    // Retrieve the value of "some_key" in for example "https://example.com/?some_key=some_value"
    let result = query.some_key; // "some_value"

Answer №2

Here's a simple method to extract the code parameter from a URL:

const getParameter = param => {
  const urlParams = new URLSearchParams(window.location.search);
  return urlParams.get(param);
}

const codeValue = getParameter('code');
copyInput.setAttribute('value', codeValue);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for creating a unit test for a React Component utilizing the useEffect hook and asynchronous fetch

Starting from a basic create-react-app setup, I am diving into the world of unit testing to enhance my skills. The goal is to update the main App component to fetch data from a local node API and display it as a string response. However, I'm encounter ...

Minimize the gap between legend text and icon in Highchart

Is there a way to decrease the space between the number and icon? I am currently working with Angular 8 and Highchart. Below is the configuration of the chart legend. https://i.stack.imgur.com/0dL7y.jpg this.legend = { align: 'center', verti ...

Utilizing jQuery to eliminate a script function from an external webpage

Currently, I am utilizing a ColdFusion script to load an external page within the container tag. This external page contains a sorting function defined as: function sorting(sortid). However, this function's sorting criteria constantly leads to errors ...

The value of useEffect does not change even after it is defined in ReactJS

I am working on a component where I need to fetch data from an API after it has been rendered, and store the response in a useState value. However, when attempting to set the state using the useEffect callback after fetching the API, nothing seems to be w ...

I am experiencing issues with the rendering of my q-btn elements in Quasar and they are

I recently started using Quasar and encountered an issue with my q-btn component buttons displaying white backgrounds at random, despite having added a background-color in the external stylesheets. Here are some examples of this perplexing problem: The e ...

Having trouble with your website div not appearing correctly on Firefox?

I'm facing an issue with the display of a website (found here). The elements inside the div with the class name of .index_container are not appearing correctly in Firefox. Despite being present, they remain invisible. I've attempted to resolve th ...

Which RxJS operators necessitate unsubscription?

It can be confusing to know which operators in RxJS must be unsubscribed from to prevent subscription leaks. Some, like forkJoin, complete automatically, while others, such as combineLatest, never complete. Is there a comprehensive list or guideline availa ...

What could be the reason behind my inability to initiate this PHP file through jQuery/AJAX?

I'm experiencing an issue with a piece of PHP code (located at ../wp-content/plugins/freework/fw-freework.php). <div id="new-concept-form"> <form method="post" action="../wp-admin/admin.php?page=FreeWorkSlug" class="form-inline" role ...

Encountering a sanity issue while attempting to execute vercel build: sanity/..page.jsx lacks a root layout. To resolve this issue, ensure that each page includes a root layout

While my project runs smoothly locally, I encounter issues when trying to deploy it on Vercel. The error message reads: ⨯ sanity/[[...index]]/page.jsx doesn't have a root layout. To resolve this issue, ensure that every page has a root layout. Comp ...

Other Components take turns submitting the form in one Component

Two components, CreateCandidate and Technologies are being used. CreateCandidate requires technologies from the Technologies Component. When passing the technologies to CreateCandidate, the form within CreateCandidate is submitted. Below is the code: Crea ...

What is the best way to execute two JavaScript functions within an inline onclick event?

I am currently working on the following code snippet: <script> function delay (URL) { setTimeout( function() { window.location = URL }, 2000); }</script> And then I have the following within the <body> element: <img src="small_rabbi ...

Maintaining Aspect Ratio and Adding Letterboxes with Next.js Image

In my Next.js app, there is a section dedicated to displaying selected photos from a gallery. It's crucial for this area to maintain a fixed size of 566px*425px as users navigate through images or when a photo is loading. While the layout is responsiv ...

jQuery does not allow for text input values to be posted

Having an issue with a form using the POST method. I have some PHP controls that are being calculated in jQuery. While all the form control values are accessible in the next form using POST, the values added through jQuery are not posting to the next form. ...

Displaying a webpage in JavaFX Scene Builder

Is it possible to display a webpage or an HTML file using JavaFX Scene Builder? Thank you. ...

Establish the directive upon receiving the broadcast

Is there a way to trigger a directive when a specific event occurs on the backend and sets a value to false?... This is where the event is being captured .factory('AuthInterceptor', function($q, $injector, $rootScope) { return { ...

What kind of output should a Server Side Component generate?

Recently, I decided to incorporate the NextPage type from Next.js into my component writing routine after hearing it's a beneficial practice. However, I discovered that it only functions properly with client-side components. When attempting to utilize ...

When I click on the md-select element, a superfluous "overflow-y: scroll;" gets added to the <body> of my webpage

Typically, I have the following styles on my body: element.style { -webkit-app-region: drag; } However, when I interact with a md-select element (you can observe this behavior on the provided link), additional styles are applied. element.style { -w ...

"Clicking on a jQuery div will cause it to slide down, and clicking again

I am currently working on this code snippet: $("#right").click(function() { $("#signin").stop().slideDown(300); }); At the moment, it drops down when clicked, but I would like it to slideUp() when clicked again. Your help is appreciated. On a relate ...

Navigating tables with Selenium WebDriver

I am struggling to locate and interact with a specific element in a table using Selenium WebDriver. The issue is that the table IDs are constantly changing, making it difficult for me to click on the desired element. For instance, consider the following t ...

collection of assurances and the Promise.all() method

Currently, I am dealing with an array of Promises that looks like this: let promisesArray = [ service1.load('blabla'), service2.load(), // throws an error ]; My goal is to execute all these Promises and handle any errors that occur, as ...