“JavaScript Obtenha parâmetros de consulta do URL” Respostas de código

JS Get Query Param

const queryString = window.location.search;
const parameters = new URLSearchParams(queryString);
const value = parameters.get('key');
MCRC

Parâmetro de consulta de leitura de javascript

// example url: https://mydomain.com/?fname=johnny&lname=depp
const urlParams = new URLSearchParams(window.location.search);
const firstName = urlParams.get('fname'); // johnny
Scriper

JavaScript Get Pardey Parâmetro

function getUrlParameter(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var results = regex.exec(location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
Grepper

JavaScript Obtenha parâmetros de consulta do URL

const getParameters = (URL) => {
  URL = JSON.parse('{"' + decodeURI(URL.split("?")[1]).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') +'"}');
  return JSON.stringify(URL);
};

getParameters(window.location)
// Result: { search : "easy", page : 3 }
Rashid Siddique

Obtenha parâmetros de consulta

import React, { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";

function CheckoutDetails() {
  const location = useLocation();
  const [amountValue, setAmountValue] = useState(1);

  // function to get query params using URLSearchParams
  useEffect(() => {
    const searchParams = new URLSearchParams(location.search);
    if (searchParams.has("amount")) {
      const amount = searchParams.get("amount");
      setAmountValue(parseInt(amount, 10));
    } else {
      setAmountValue(1);
    }
  }, [location]);

  return (
  	<p>Amount: {amountValue}</p>
  )
  
Inquisitive Iguana

Respostas semelhantes a “JavaScript Obtenha parâmetros de consulta do URL”

Perguntas semelhantes a “JavaScript Obtenha parâmetros de consulta do URL”

Mais respostas relacionadas para “JavaScript Obtenha parâmetros de consulta do URL” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código