2

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.

1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for reply, but the result is Query variable key not found.
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")

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.