Dynamic ASP.NET script file

As someone who is brand new to ASP.NET, I am facing a challenge with a JavaScript file where I want to input values dynamically upon page load. In my attempt to test this feature, I used the following code:

$
(
    function () 
    {

        alert('<% = DateTime.Now.ToString("dddd, MMMM d, yyyy") %>');
    }
)

The alert successfully triggers upon page load as expected, but the VB script does not execute; instead, the VB code gets echoed back as if it were simply treated as another string.

This script file is declared on the site master page right before the closing body tag.

I'm not sure what mistake I might be making here. Any help would be greatly appreciated. Thank you.

Answer №1

The server side script code enclosed between <% and %> tags needs to be parsed by the server in order for it to be executed. For example, in Asp.Net, .aspx files or in MVC, .cshtml and .vbhtml files are parsed by the server before the code is executed and the page is sent to the client.

Unlike server side scripts, javascript files are not parsed by the server, which means the server side code will not be executed before being sent to the client. This results in the browser and javascript engine seeing exactly what you typed, as seen in the alert.

If you want the serverside code to be executed before sending javascript to the browser, the best approach is to include the javascript in your .aspx file (for webforms) alongside the html content.

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

Transferring an array to the server-side with the help of $.getJSON

Utilizing $.getJSON() function to send data to the server side (PHP, Codeigniter) in the form of an array and using the returned data for further processing. Issue: Sending an associative array to the server does not yield any results on the server side. ...

Getting JSON with duplicate keys in JavaScript can be achieved by parsing the data using a custom function

I am attempting to retrieve JSON from a URL, but I have encountered an issue where the response object is removing duplicate keys. Is there a way to fetch the JSON without eliminating these duplicates? Below is my JavaScript code: $('document'). ...

When attempting to access AJAX JSON properties using an index within a promise, the result returned is undefined

I have a quiz app that communicates with my server's API using MongoDB. I am trying to access the response indexes in this way: setTimeout(() => { axios.get('/api/ninjas') .then(function (questions) { var data = questions.d ...

NodeJS Puppeteer encountering problem with setting download behavior

When attempting to specify a custom download path, Chrome seems to ignore the downloadPath option and continues to save files in the default C:/Users/Me/Downloads folder regardless. const puppeteer = require('puppeteer'); (async () => { c ...

Utilizing string interpolation within the parameters of an AJAX call

I have a locale variable that can hold values such as en, fr, or es. The goal is to include this locale key in an AJAX request as part of the data parameter. let locale = "en"; let name = "example"; $.ajax({ url: "/path", method: "PUT", data: {chara ...

What causes this exception to be thrown while running a basic insert query?

I am facing an issue with this code that performs an insert query. The problem arises when attempting to execute the query, as it throws an exception: _strSQL = "INSERT INTO Cpe ( DateAdded, [Cpe] "; strSQLParametri = " VALUES ( GETDATE(), @CPE "; addPar ...

Issue with SetTimeout not functioning properly on initial use within socket.io

Currently, I am trying to come up with a method to retrieve an IP address from a node server that is hosted on a dynamic IP. The issue I am facing is that the setTimeout function does not seem to work properly on the server side during the initial launch. ...

Determining whether a path is absolute or relative: A step-by-step guide

Is there a universal function in node.js that can determine if a given path is absolute or relative? Unlike Windows, which starts with 'C:' or '\', UNIX paths begin with '/'. ...

Tips for syncing input field values with mui slider?

Implementing a MUI slider that ranges from a minimum of 8 to a maximum of 255. <Slider value={range} min={minVal} max={maxVal} onChange={handleSliderChange} valueLabelDisplay="auto" /> In addition, displaying input fields to show ...

What is the best way to organize components in ReactJS?

Confusion I am seeking clarification on the placement of my aside code. It is meant to render components such as searchInput and menuStudy. I was considering placing it in the container's folder, but it does not serve any specific purpose like hand ...

Transferring Specific Information to Individual Fancybox Instances

I'm currently in the process of integrating Fancybox 3 into my application. My goal is to create a custom button that performs specific functions for each instance, requiring additional data such as photoId and settingsId. To illustrate: HTML: Withi ...

Tips for initiating a function at a designated scroll point on a webpage

Currently, I am testing out a code snippet that allows images to flip through in a canvas element. I'm curious if it's possible to delay the image flipping effect until the viewer scrolls down to a specific section of the page where the canvas i ...

What methods can I use to determine the height of an Angular directive?

For over an hour, I've been trying to accomplish a seemingly simple task: creating an Angular directive that can both retrieve and set the height of the current element. Additionally, this directive should be able to access the browser window in order ...

How can I trigger an event in Vue.js when a selection is made in a dropdown menu?

Here is an illustration fiddle: https://jsfiddle.net/40fxcuqd/ Initially, it shows the name "Carl" If I choose Carol, Clara, etc., an event will be triggered and data will be logged to the console. However, if I click on the dropdown and select "Carl" ...

What is the method to display my input value within my application interface rather than triggering an alert popup?

Hi there, I am seeking assistance with my GUI code. I want the input value to be displayed as text on the screen, or even better, to look like a chatroom so that I can integrate a simple chatbot later on. My main goal is to have the texts show up in the bl ...

Determining the perfect duration for Outputcache: tips and tricks

I recall reading that a high traffic website should aim for a session duration between 30 to 60 seconds. However, it seems difficult to find concrete documentation on this topic. Many examples I've come across suggest durations of one to a couple of m ...

Issues with Masonry at Specific Screen Sizes

I've successfully implemented masonry on my website, but I'm encountering issues with how it displays at certain screen resolutions. I have double-checked the code and ensured that all margins and widths are correct. Is this a common problem with ...

Dynamic pop-up content based on AJAX response

I don't have much experience in web development, so please excuse any beginner mistakes;) Currently, I'm working on creating a dynamic popup window in HTML, which is hidden by CSS. When a button is clicked, an AJAX post request is triggered to r ...

Creating 3D shapes with ExtrudeParametricGeometry in ThreeJS

Is it possible to combine ExtrudeGeometry with ParametricGeometry? For example: function customGeometryFunction(u,v){ var x = -(width/2) + width * u; var y = -(height/2) + height *v; var z = (Math.sin(u*Math.PI))* -20; return new THREE.Vector3(x ...

Storing the created .wav file on the server using PHP

Is there another way to handle this? How can I manage streaming of a WAV file? I'm currently working on a platform that allows users to create their music compositions and the system outputs a .wav file for each creation. While I can play the mus ...