What is the best way to include dynamic query parameters in the src attribute of a <script> tag?

As I develop a basic asp.net application, I encountered a situation where I needed to include a script with a dynamic query parameter in my aspx page.

For instance:

<script src="../javascript/script.js?v=#var#" type="text/javascript"></script>

In the given code snippet, the script path allows for different query parameters to be inserted instead of #var#.

I attempted to extract the parameter value from code behind using the following code.

<script src="../javascript/script.js?v=<%# myVar %>" type="text/javascript"></script>

However, <%# myVar %> returns an empty value. By substituting = for #, the code works perfectly when the script reference is added at the end of the page.

Nevertheless, this approach only functions when the script is referenced at the bottom of the page; otherwise, an error will be thrown.

"The Controls collection cannot be modified because the control contains code blocks (i.e. `<%= %>`)."

Thus, my query remains, "Is there an alternative method to achieve the same outcome?"

Answer №1

If you are using ASP.net instead of MVC, you can create a dynamic script tag by using code behind. Here is an example of how to achieve this:

protected void Page_Load(object sender, EventArgs e) {
    string jScriptValidator;
    jScriptValidator = "<script type='text/javascript' src='../javascript/script.js?v=#123'></script>"; // your dynamic script tag with dynamic parameter         
    Page.RegisterStartupScript("key", jScriptValidator);
    }

The result will be similar to this:

https://i.sstatic.net/GppaB.jpg

I hope this solution works for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process of choosing a language (English/French) within a pop-up?

<body style="text-align:center"> <h2>Popup</h2> <div class="popup" onclick="myFunction()">Interact with me to show/hide the popup! <span class="popuptext" id="myPopup">A Simple Popup!</span> </div> <script& ...

Vite terminal not executing Npm commands

Here is what I entered: npm run dev Error: npm ERR! Missing script: "dev" npm ERR! npm ERR! To see a list of scripts, run: npm ERR! npm run npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\andre\AppData&bs ...

Modifying paragraph content with JavaScript based on selected radio button values and troubleshooting the onclick event not triggering

I am working on implementing a language selection feature on my website where users can choose between English and Spanish. The idea is to have two radio buttons, one for each language, and a button. When the button is clicked, the text of the paragraphs s ...

unresolved string constant issue with a django template command

I encountered an issue with the code snippet below, which is resulting in an unterminated string literal error: $(function() { $('#addDropdown').click(function() { var $d = $('{{ form |bootstrap }}').fadeIn(). ...

Expanding the Number of Arguments Sent to a Callback Function

I have a scenario where I am using a method that sends a POST request and then triggers a specific callback function to manage the response: myService.verify(id, verificationCallback); function verificationCallback(err, response) { ... } My query is two ...

Passing the contents of a datatable as parameters to a PHP script

I am facing a challenge with my datatable that has two columns, "Name" and "Age". After populating the datatable using Ajax, I create a button for each row. The goal is to send the "Name" and "Age" fields of the clicked row to a PHP script, which will then ...

Discovering the right place to establish global data in Nuxt JS

Exploring the world of NuxtJS today, I found myself pondering the optimal method for setting and retrieving global data. For instance, how should a frequently used phone number be handled throughout a website? Would utilizing AsyncData be the most effecti ...

Using JavaScript to manage form input values in React

I am currently coding a basic application using NextJS and bulma CSS. The snippet below shows the form I am working on: const MyPage = () =>{ const [firstName, setFirstName] = useState('') const [secondName, setSecondName] = useState('&ap ...

Adding list items to an unordered list without making them draggable using jQuery

How can I allow users to build a list of sports and drag them into a "cart" using jQuery? The issue I'm facing is that the appended list items are not draggable, and I'm unsure of how to solve this problem. /* JS code for sports.html */ $(fu ...

Pair HTTP POST data with a Dictionary<string, string[]>

Encountering an issue with parameter mapping for an ASP.NET MVC action. When posting data from the browser, it appears like this: { "query" : "some search phrase", "faceting" : { "colors" : ["red", "blue"], "brands" : ["Fantastic"] } Th ...

Having trouble displaying real-time camera RTSP streaming using Angular

I am currently in the process of developing a web application using Angular and I need to incorporate a window that displays live RTSP streaming. Upon conducting research, I discovered that this can be achieved by utilizing the JSMpeg JavaScript library. ...

What is the best way to incorporate multiple button fields in a single column in an ASP.NET GridView?

Looking to add multiple actions, such as "Approve", "Deny", and "Return" to a gridview? You can achieve this by combining the buttons into a single column: <asp:ButtonField ButtonType="Link" CommandName="Approve" Text="Approve" /> <asp:ButtonFi ...

Tips for updating an anchor link within a Textarea using jQuery

I have a simple inquiry about jQuery. I am interested in changing the "a" tag within a Textarea. I understand that "a" tags do not work within a textarea, which makes it impossible to target them with jQuery selectors. For example: <textarea><a ...

When the next button is clicked, the button content disappears

I am struggling with a problem involving two buttons that store tables. The issue is, I want the table to disappear when the second button is clicked and show the contents of the second button immediately after clicking it once, rather than having to click ...

Monitoring page reload with JavaScript

Here is an example of tabbed content: <div class="tab"> <button class="tablinks" onclick="openCity(event, 'NewYork')" id="defaultOpen">New York</button> <button class="tablinks" onclick="openCity(event, 'LosAngeles& ...

The ng-view directive appears to be missing in the AngularJS framework

I am attempting to create a route using AngularJS, but when I launch my application, the ng-view does not display anything. I am new to AngularJS. index : <!DOCTYPE html> <html lang="en"> <head> <title>CRUD</title> </ ...

initiating a function on a separate webpage using ajax

Our website includes an IIFE script from a third party to check for a specific URL parameter and set a cookie. However, on another page, we need to set this parameter without redirecting the user, reloading the page, or altering the third-party code. The ...

Making a Jquery 1.10.2 cross-domain request

I am in need of assistance with the following code snippet: function doPoolPartyGetChildrenAjaxRequest(parent) { return $.ajax({ url: "http://127.0.0.1:8086/PoolParty/api/thesaurus/1DCE2E49-7DD8-0001-524C-1A1B14A0141A/childconcepts", data: {lan ...

Switching back and forth between fluid text fields

Is there a way to navigate between dynamically generated textfields using an iterator from struts-tags? <s:iterator value="aList"> <td width="50px" align="center"> <s:textfield name="solField" size="2" maxlength="1" style="text-transform ...