-1

I have an external JavaScript file that I load from my HTML pages in the usual way (i.e. by using a script tag that as its src attribute provides an URL to the JavaScript file). In that URL, I want to include a query parameter and from within the JavaScript have access to the value of that query parameter. Note - I have found answers to how to handle URL query parameters from a URL in general, but note that this question concerns one specific URL, namely the one that is used to load the JavaScript file in question, compare to using argc and argv in C.

5
  • 2
    document.currentScript? Commented Apr 11, 2023 at 14:30
  • Can you provide example URL? If your HTML is /page.html?param1=foo and it links to the JS /script.js?param2=bar are you interested in getting param1 or param2? Commented Apr 11, 2023 at 14:33
  • Comparing this to argc/argv is fairly pointless, since the execution mechanism is completely different. It'd help if you provided more context here, so we can propose an actual solution rather than taking stabs into the dark. Commented Apr 11, 2023 at 14:34
  • @StephenOstermiller I was interested of get the value of param2. Commented Apr 12, 2023 at 12:58
  • @deceze The mentioning of argc and argv is merely a comparison. Commented Apr 12, 2023 at 13:01

1 Answer 1

0

If your calling document has this data, put it in a JavaScript variable, then use this in the incorporated code via, for example, a function call.

const value = ...;

imported(value)

Remember, the JavaScript itself only runs in the current document, not on some remote machine doing mysterious remote machine things.

There is no argv equivalent since the JavaScript is normally just a static file, the server it's requested from does not care about the contents in any way, and does not run it. That's your job when you incorporate it via <script>, you're the caller.

I'm expecting this is your implied approach:

<script src="https://example.com/script.js?value=..."></script>

Sending it in as a GET parameter is completely pointless, the server you're fetching the static JavaScript file from likely doesn't care. They get ignored. I've only seen these used for "cache busting" to ensure the most recent version of the file is loaded.

You could also keep the parameter as a data property on some element.

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

4 Comments

Could you please elaborate on your code example? Why should both statements go into my JavaScript file -- it does not know that value. The code example calls a function imported with argument value, but I do not have that value.
Where does that value come from? It has to come from somewhere, and once loaded, you'll presumably have access to it. Your example is frustratingly thin on specific technical details and code examples.
The values come from clients' HTML code. They should be able to incorporate my JavaScript, which should be able to be configured based on the clients' needs, without any change to the JavaScript code. Example: one client may want some maximum number to be 7; another client may want the same maximum number to be 5; and so on. So no, I am not the caller. I thought of query parameters, but am open to any suggestions achieving the objective. I gave no code since my question is about how to meet an objective. Code is the answer to my question. So, please elaborate your code.
Normally if a client is using an external JavaScript library they must either initiate a call, as you might do with an analytics extension, set up DOM elements in a certain way (e.g. data properties or other attributes), or both. You can't communicate anything of value in the src property.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.