1

any way to get url parameter that passed to js it self ?

example:

https://xxxxxxx/sample.js?key=valuehere

the result i want to get: var key = valuehere

like php, it can be done with $_GET, but how it work it js ?

thanks in advance

Edit:

need to define the key in the js it self

function renderMe () {
    var value = getkeyvaluefromURL; // query param stored here
    $.ajax({
        url: base_url+"/api/render",
        method: "POST",
        data : { 'command' : 'list', 'key' = value},
        dataType: 'JSON',
        success: function (data) {
            $('#list').html(generateBoxList(data));
        },
    });
}

so the value need to passed to ajax query when it stored to js url. all is one file js.

  • Does this answer your question? https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters – dale landry Feb 01 '21 at 03:37

3 Answers3

0

You have access to the querystring from window.location.search

These answers demonstrate how to parse it to extract data: https://stackoverflow.com/a/2091331/4088472

Slava Knyazev
  • 2,557
  • 1
  • 17
  • 33
0

By using the below code you get the search parameters and are then able to search for the query by name

const params = new URLSearchParams(window.location.search)
const key = params.get("key")
Sean Reilly
  • 685
  • 14
0

The closest thing I can think of for URL parameter parsing/passing would be using the window.location.search property.

www.example.com/?page_type=home%20page&color=black&size=xxxxl

const queryString = window.location.search;

console.log(queryString);

Results: ?page_type=home%20page&color=black&size=xxxxl

Sources: https://easyautotagging.com/javascript-get-url-parameter/ https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams