Add the parameter value from the URL to the HTML code

To retrieve the username from a URL and display it in HTML

Example URL

something.com?editors=username

HTML code snippet:

<P id="demo"><p>

Javascript function:

document.getElementById("demo").innerHTML =
"Welcome " + get_editor() + " to my website";

function get_editor() {
  var url = new URL( window.location.href );
  var params = new URLSearchParams(url.search);
  return params.get('editors');
}

Answer №1

If you're dealing with a GET parameter, you can handle it like this:

(function() {
  var url_string = window.location.href;
  var url = new URL(url_string);
  var editors = url.searchParams.get('editors');
  var htmlElement = document.getElementById("element");
  htmlElement.innerHTML += editors;
})();

Make sure to replace searchParams with your specific parameter (n in this case).

Once you have retrieved the element, you can easily add your HTML code.

UPDATE: I've included an example of appending an HTML element.

Answer №2

Although I may be coming in a bit late, after reading Czeran's response, I decided to put together this JavaScript function that appends a value to the URL and navigates to it:

    function appendToURL(value){
        var currentURL = window.location.href;
        currentURL += "/" + value;
        window.location.href = currentURL;
    }

This feature allows for a more customized URL modification.

I hope you find this helpful!

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

How can I make an AJAX request in Rails 3 to retrieve an HTML page?

I am experimenting with using Rails to display an .html.erb template via an ajax request under specific conditions. By default, I am consistently receiving the .js.erb file when making an ajax request. Without delving into the reasons behind this choice, ...

Creating a new line in VBA HTMLBody with long text can be challenging

I am struggling with separating the code in my macro into multiple lines. Each time I attempt to do so, an error message pops up. The error message says: "Expected: line number or label or statement or end of statement". A quotation mark is automatica ...

Receiving HTML codes through AJAX requests using .htaccess

retrieving html codes after ajax post request on the client side: $('#bgn').live('click',function(){ var data = {action:'today'}; $.post('inc/ajax-handler.php',data,function(response){ $('#result&a ...

Can inter-portlet communication be achieved using AJAX?

Is it possible to use an AJAX call in Portlet A to dynamically update Portlet B without refreshing the entire portal page? I am aware that portlets can refresh their content using the JSR286 resourceURL tag, but I am curious about targeting and updating a ...

Is there a way to implement a feature that automatically closes the chatbot when the user clicks outside of it?

Here is an example of my HTML code: <div class="chatbot chatbot--closed "> <div class="chatbot__header"> <p><strong>Got a question?</strong> <span class="u-text-highlight">Ask Harry</span></p> < ...

Dependency injection in Angular 2 pipes

Greetings everyone! I'm a newcomer to angular 2 and currently trying my hand at creating a custom pipe/filter. However, I've encountered an issue when attempting to inject the pipe I created inside app.ts as shown in the image linked here. Here ...

Utilize jQuery to encase the final word of a paragraph within span tags

Greetings! Consider the following HTML snippet: <p>Phasellus sit amet auctor velit, ac egestas augue.</p> I am interested in enclosing the last word within span tags to achieve the following result: <p>Phasellus sit amet auctor velit, ...

Having trouble removing a DOM element with jQuery?

Hey there, I need your help with a jQuery issue I'm facing. I am currently working on cloning a div element that contains a span with a click event attached to it. The purpose of the click event is to remove the newly cloned section from the DOM. Whil ...

Track hovering without the need for an 'out' event multiple times

I am currently developing an application that utilizes the HTML 5 canvas to display a grid using lines on the canvas. My goal is to enable the user to hover over a cell in the grid and receive the cell coordinates through a tooltip. Currently, I am logging ...

How to dynamically inject HTML content from a child component into a different component using Angular 5

Is it possible to customize the content of a reusable header section based on the current route data in Angular? The header currently displays a title and description pulled from the route data property. My concern is how to dynamically inject different H ...

There appears to be an issue where the session object cannot be retrieved by the Struts2 action

I have a struts2 action that is invoked by a JavaScript function. The JavaScript function uses uploadify to enable multiple file uploads: <script type="text/javascript"> $(document).ready(function() { $("#fileupload").uploadify({ ...

What is the best way to establish and maintain lasting connections with the Firebase database while utilizing the superagent

Currently, I am following the Firebase Functions documentation on enhancing Firebase database performance. I have provided the code snippet below for your reference. const request = require('superagent'); const functions = require('fireba ...

Creating a custom fav icon within a button with CSS styling

https://example.com/code-example Hey there, I'm attempting to replicate the button showcased in the code example above. However, I'm facing a challenge when trying to incorporate the stars without altering the existing script. Another issue is w ...

404 error occurs when Spring-Boot fails to include CSS file through ResourceLocations

My spring-boot application is currently running smoothly on a local computer, but I have encountered an issue. When I run mvn package, none of my CSS or JavaScript files located in /src/main/wepapp/css are included in the jar file created in the target d ...

Place the "Close" button outside of the div container

I am facing an issue with the login button protruding slightly outside the border of a div. Despite trying to adjust the code to align it correctly, the issue persists. .closeWindow{ float: right; margin-left: 15px; /* half the width of the close b ...

Alternative way to search for child elements within an element without the use of jQuery

I am in the process of creating a universal set of functions to verify the existence of children with specific attributes within a particular element. Unfortunately, I do not have access to jQuery for this task. Here is an example function call: has_chil ...

Passing a variable through jQuery onClick event to a URL via GET request and subsequently loading the content of

After successfully passing variables through AJAX calls via onClick to a PHP file and loading the results on the initial page, I now face the challenge of passing a variable analogously via onClick to a PHP file but this time I need to either open a new wi ...

I am disappointed with the lack of functionality in Angular's HTML type inference

When working inside an Angular component, I want to select a div element by id or class. This method works menuDisplayDiv = document.getElementsByClassName("some_class")[0] as HTMLDivElement; menuDisplayDiv = document.getElementById("some ...

The 'length' cannot be searched using the 'in' operator

I am experiencing an issue that I can't quite solve. I have come across solutions for this problem, but they all involve methods that don't use AJAX to retrieve data. If anyone can assist me with this, I would greatly appreciate it. Here is the J ...

The transformation of button styles created with CSS and PNG images

Looking for a streamlined way to style buttons? I have a class called .button-small that sets the colors and dimensions: .button-small { background: linear-gradient(to bottom, #59aeee 1%,#4ea0dc 100%); /* W3C */ border-radius: 3px; cursor: pointer; width: ...