Storing Global Javascript Variables in an ASP.NET Application: Best Practices

I am currently facing a challenge with using a global variable in JavaScript within an asp.net application.

Within my code, I have a button that triggers the registration of a script:

     Page.ClientScript.RegisterStartupScript(Me.GetType(),"showModel",String.Format("<script>showModel('{0}', '{1}');</script>", objID, token))

This script contains a globally defined variable. However, when attempting to call another function on the same script using a similar method, the global variable is returning null. Is there a different approach to invoking the script after it has been registered?

Answer №1

If you want to execute the script that is registered in your code, it's important to declare your script as a function so that you can call it later in your code:

function performAction(){
   displayWindow('{0}', '{1}')
}

//Later in your code:
performAction()

If not, the term "showModel" in your code represents the name of the registered script. When you mention a global variable, what specific variable are you referring to?

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

How can I add margins to a Bootstrap v5 column that spans the full height and width of the content?

In this scenario, I have two rows and two columns in each row. My goal is to make the yellow blocks take up all available space in the column while having a margin. However, currently the margin disrupts the alignment of the yellow content. https://i.ssta ...

Troubleshooting a Node.js Application Deployment Issue on Heroku

2017-09-03T18:50:57.000000+00:00 app[api]: Build initiated by user [me] 2017-09-03T18:51:28.776809+00:00 heroku[web.1]: State transitioned from crashed to starting 2017-09-03T18:51:28.572116+00:00 app[api]: Deploy 20b0544e by user [me] 2017-09-03T18:51: ...

Error: Preflight request returned a 405 HTTP status code when querying Ionic + CI backend

I am currently working on my first app using ionic with a codeigniter backend. However, I am encountering the "Response for preflight has invalid HTTP status code 405" issue in ionic + CI backend. Can anyone help me solve this problem? This is my controll ...

Implementing pagination in an express application

I am faced with a situation where I have an array of JSON objects generated by a function in my running Express app. When there is only one object, it is rendered using the following Jade view. p You searched for '#{prop}' h2 Result if res ...

Vue mouseup and mousedown event not functioning as anticipated

I am experiencing an issue with my code where the button is supposed to be disabled on mousedown event and enabled back on mouseup event. However, for some reason, the button does not get enabled on mouseup event. Here's the snippet of my code: ne ...

What is the best way to organize a list based on its textual content, which may consist of numbers

I am attempting to organize this list based on the distance (56km, 96km, 6km) from lowest to highest. I realize there are simpler ways to handle and add the li's to the ul, but I specifically require it in this format. The issue is that I am completel ...

In every scenario, the loading spinner is displayed on Reactjs routes

Within my small application, I have set up various routes that load different components based on the path. One issue I encountered is with the ProductsPage container, where I have a loading spinner to display while waiting for a response from the database ...

When attempting to open an Excel file, you may receive a message stating that the file format and extension do not match, indicating that the file could potentially be

My website, built in Asp.net, has the following code behind: Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click Response.Clear() Response.AddHeader("content-disposition", "attachment;filename=FileName. ...

Vanish Dropdown upon clicking Using JavaScript

Hey, I'm new to web development and I'm currently working on creating a web app for iPhone. Everything is going smoothly except for one issue - my dropdown menu works perfectly on desktop Chrome, but on the iPhone's Safari browser, I can&ap ...

Viewing items in the WebStorm console using Nodejs

I love how WebStorm allows me to simply hover my mouse over an object in the code and inspect it right away, just like this: https://i.sstatic.net/7ra9R.png But I wonder if there is a way to do this from the console too, similar to Google web tools? Unf ...

Angular 17: Issue with _HttpClient Provider Not Found in Standalone Component Utilizing ApiService

I have been developing a cutting-edge Angular 17 application that integrates the Spotify API using the innovative standalone component functionality. However, I am facing an issue while attempting to inject the HttpClient into a service. Despite meticulous ...

Ways to update the div's appearance depending on the current website's domain

There is a piece of code that is shared between two websites, referred to as www.firstsite.com and www.secondsite.com The goal is to conceal a specific div only when the user is on secondsite. The access to the HTML is limited, but there is an option to ...

Insert a value within each iteration of ng-repeat

Is it possible to iterate over a set of items and increment the value of X by 10 each time it is repeated? For example: <rect ng-repeat="a in b" x="0" y="0" fill="#00000" width="206.2" height="117.7" class=" {{ a.name }} " /> I am looking to achie ...

What is the significance of ASP.NET Error CS1026: Missing ")"?

What could be causing the CS1026 error, indicating an expected character at the line below? <%=Html.BeginForm("AddAdvertisement", "Advertisement"){%> //here hello <%} %> ...

The upload method in flowjs is not defined

I am a novice when it comes to flow.js and am currently using the ng-flow implementation. I have a specific task in mind, but I'm unsure if it's feasible or not, and if it is possible, how to achieve it. I've created a factory that captures ...

Unable to locate javascript frame using webdriver

Recently, I encountered an unfamiliar issue while working on a Java program that checks store availability on websites like Staples. The problem seems to be related to entering information into a textbox found in a pop-up JavaScript window that appears whe ...

Fill in the form using JSON data by matching the names with their corresponding values

Here is how I am collecting and storing the data from my web form as JSON, utilizing the name attribute and values. What is the best way to populate the form again with the saved JSON data in the same structure using name and value? (context: I will keep ...

I'm having an issue where I'm trying to post form data using AJAX, but for some reason, the form continues to submit and refresh the page. Can anyone

Recently, I have been working on a small and simple chat application using mainly JQuery and AJAX technologies. Below is the HTML form I have created for this chat application: <form class="chat_form" method="post" id="chat_form" autocomplete="off"> ...

What exactly is the data type of setInterval in TypeScript?

If I want to define the type of a variable that will be used with setInterval in the following code snippet: this.autoSaveInterval = setInterval(function(){ if(this.car.id){ this.save(); } else{ this.create(); } ...

"Efficiently fetch data with an Express GET request without the

My goal is to send the client the HTML page (create.html) in response to a GET request triggered by a button click using fetch. I am intentionally avoiding the use of a form due to formatting and potential scalability issues. The code acknowledges that the ...