logo

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ
No Result
View All Result
logo
No Result
View All Result
Trang chủ how to get querystring from url using jquery

How to get querystring from url using jquery

by Admin _ July 30, 2022

DISCLOSURE:This article may contain affiliate liên kết and any sales made through such links will reward us a small commission,at no extra cost for you. Read more about affiliate Disclosure here.

Bạn đang xem: How to get querystring from url using jquery


Sometimes we need to get values through the query string in the URL on the browser address bar. But the question is how to get them in JavaScript or jquery because we need them on the client-side.

Here I’m mentioning 4 ways for getting values from query parameters. I have also displayed uses examples. You can use either of them as per your choice & need in the client-side JS or jQuery.


*

1. Retrieve Query String Values using URLSearchParams

This is the simplest and most recommended way khổng lồ retrieve values from the query string. URLSearchParams is a built-in interface in the latest browsers with a number of methods khổng lồ get and manipulate values.


const urlParams = new URLSearchParams(window.location.search);const param_x = urlParams.get("param_x");/*Examples://Query string present in the browser address bar is "submitted=true&msg=success"var urlParams = new URLSearchParams(window.location.search);var has_msg = urlParams.has("msg"); // true (boolean)var msg = urlParams.get("msg"); //successvar submitted = urlParams.get("submitted"); // true (string)var redirect = urlParams.get("redirect"); // NULL*/

While this interface supports a number of decent browsers, it is not available for all, especially old ones. I suggest checking the official documentation on Mozilla & Chrome. It has various useful methods to utilize.

2. Regular Expression in JavaScript

Here is the function which will safely decode URL components using a regular expression. We can call this function và pass the query string parameter’s name as an argument to obtain the value.

Xem thêm: Shop - Đồ Chơi Người Khổng Lồ Xanh Hulk Có Đèn Cy


Suppose we have the URL as follows: http://example.com/sports?game=football.

If we wish lớn retrieve the value for the game query parameter, we can pass it khổng lồ the qsp variable as a string & we will get football as value. If the parameter doesn’t exist then the function would simply return an empty string.

3. Simple JavaScript Function to get Query Parameters Values Object

We can also get the whole query string as an object and then access the value of a particular parameter by its key.


function get_query(){ var url = document.location.href; var qs = url.substring(url.indexOf("?") + 1).split("&"); for(var i = 0, result = ; i

The function above performs the following steps:

Getting the URLParsing query string by splitting the URLLooping through each và every key in the parsed query stringStoring every key & its value as an objectReturn the object

Below is an example:


// Query string in the URL// x=5&y&z=hello&x=6var result = get_query();console.log(result);//Output x: "6", y: undefined, z: "hello"

4. JQuery Way to Get Query String Values from URL Parameters

If you want a jQuery way lớn get a specific URL parameter’s value then here is the function.


Hope these 4 functions will help you lớn get values of query string parameters in a URL. If you have any queries or any jQuery/JavaScript issue then feel không tính tiền to liên hệ us.


WE"RE AVAILABLE

*
AS Tech Solutions is an apk Apps and Web Design và Development company, offering the following services:


API IntegrationContent WritingSpeed OptimizationAJAX ImplementationE-Commerce SolutionsASP.NET website ApplicationsAndroid Apps DevelopmentWordPress Related ServicesWindows Apps DevelopmentWebsite Design & DevelopmentTroubleshooting & customizationReal-time Applications Development
*

Hey buddy

Share Tweet Linkedin Pinterest
Previous Post

Scroll to div jquery w3schools

Next Post

Get first child selector jquery

CÙNG CHUYÊN MỤC

jquery file upload (hayageek plugin) error case with json from php

Jquery file upload (hayageek plugin) error case with json from php

28/04/2021
search/filter table rows based on input

Search/filter table rows based on input

28/04/2021
jquery form validation before ajax submit

Jquery form validation before ajax submit

18/03/2021
jquery multiple select dropdown

Jquery multiple select dropdown

28/04/2021

12/08/2022
how do i get the text value of a selected option?

How do i get the text value of a selected option?

05/08/2022
top 19 jquery datatable load data ajax example mới nhất 2022

Top 19 jquery datatable load data ajax example mới nhất 2022

04/08/2022
smooth scroll to div id jquery

Smooth scroll to div id jquery

04/08/2022

Newsletter

The most important automotive news and events of the day

We won't spam you. Pinky swear.

Chuyên Mục

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ

News Post

  • How to install php 5

About

Chúng tôi tạo ra trang web nhằm mục đích mang lại kiến thức bổ ích cho cộng đồng, các bài viết được sưu tầm từ nhiều nguồn trên internet giúp mang lại kiến thức khách quan dành cho bạn

©2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng

Liên Hệ - Giới Thiệu - Nội Quy - Bảo Mật

No Result
View All Result
  • Trang chủ
  • Chuyên mục
    • Hỏi Đáp
    • Kiến Thức
    • Sức Khỏe
    • Tử Vi
    • Công Nghệ
  • Lưu trữ
  • Liên hệ

© 2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng.