Is it possible to include alligator tags(<% %>) within a JavaScript string?

I am attempting to redirect a page by pulling the url from the config file.

However, when I try the following code:

   <script type="text/javascript">
<%string redirectUrl = System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"];%>
    window.parent.location.replace("<%=redirectUrl%>");
</script>

the alligator tags <% %> are not being highlighted, and I am receiving the following error in the yellow screen:

the controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

What am I doing incorrectly??

Thanks!

Edit:

It does work if I directly insert the url into the code, like this:

window.parent.location.replace("http://theurl.com");

However, I need to dynamically change the url depending on other factors, so I require it to be in the config file :S

Answer №1

It appears that you may have placed the block within a

<head runat="server"> ... </head> 

tag. If you wish to utilize <% %> blocks, you will need to eliminate the runat="server" attribute from the head tag. However, this action will result in the loss of functionalities such as Page.Title.

To resolve this issue in your specific scenario, you can do the following:

window.parent.location.replace("<%= System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"] %>");

This adjustment should resolve the issue by removing the <% %> tags.

Answer №2

I have encountered this issue on multiple occasions. The challenge lies in ASP.NET not being able to determine where to position certain controls it generates within the control hierarchy. One workaround I have found effective is to embed code directly within the server control, like so:

<asp:PlaceHolder runat="server">
    <script type="text/javascript">
        window.parent.location.replace("<%=System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"]%>"); 
    </script>
</asp:PlaceHolder>

Answer №3

Give this a shot:

 <script type="text/javascript>
    window.parent.location.replace("<%=System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"];%>");
</script>

Answer №4

   <script type="text/javascript"> 
    window.parent.location.href = "<%=System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"]%>"; 
</script> 

Answer №5

private void InitiatePage(object source, EventArgs args)
{
   Page.DataBind();
}

Then add the following code to your page:

<script type="text/javascript>
   window.parent.location.replace('<%#System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"];%>');
</script>

Answer №6

give this a shot

<script type="text/javascript">
<%= let redirect = System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"] %>
window.parent.location.replace("<%=redirect%>");
</script>

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

Troubleshooting Issues with Passing Values in jQuery AJAX POST Requests

Currently, I am working on two basic PHP pages: notification.php <html> <head><title></title> <meta charset="UTF-8"> <script src="https://cdn.firebase.com/js/client/2.4.2/firebase.js"></script> <script src="ht ...

How can I resolve the ReferenceError in NextJs which states that Audio is not defined?

Recently, I implemented a Next component that acts as a music player, allowing users to play or stop the audio just like an MP3 player. While the functionality works perfectly fine on my local server – clicking the button triggers the audio play or pause ...

Is it possible to combine the outcomes of a SQL query that produces numerous entities with identical identifiers?

Currently working on my first Node.js API project. Within the '/posts' endpoint, I am receiving data in this format: [ { "POST_ID": 1, "POST_TITLE": "Post N.1", "POST_DESCRIPTION" ...

Instantaneous data refresh using Firebase Cloud Functions

Currently, I am working on developing a chat feature in React using Firebase cloud functions. However, I am facing an issue where I do not receive updates when a user sends a message. Below is the code snippet that I have been working with: const app = a ...

What is the method used by Bootstrap to create a darker page background behind a modal?

I am struggling to understand how Bootstrap darkens the background of a page when a modal is displayed. Even though a class is added to the body tag (modal-open), the effect on the background isn't clear to me. Learn more about Bootstrap modals here ...

Restful Spinner

app.config(function(RestangularProvider) { RestangularProvider.addRequestInterceptor(function(element) { console.log("Request initiated"); return element; }); RestangularProvider.addResponseInterceptor(function(data) { ...

Whenever jsonp is used in conjunction with Ajax, an error is always returned

I have dedicated my day to understanding how jsonp operates. Despite trying various solutions found online, none seem to work for me when accessing a third-party REST API. Initially, I used dataType: "Json" which resulted in a cross-domain blockage er ...

Ajax sends the URL location to Python

I'm attempting to piece together some code. There are two distinct functions that I am trying to merge into a single entity. Code snippet: <!DOCTYPE html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> &l ...

PHP functions triggered by ajax fail to function properly when called repeatedly

I have encountered an issue with my javascript function that calls a php script upon clicking a button or link to retrieve data. Everything works well, except when I attempt to call data from an already outputted content via the same function. Let me share ...

How can I write the code to enable dragging the button for navigating to the next page?

<a draggable="true" class="user" id="leonardo" ondragstart="dragUser(this, event)" aria-selected="undefined"> IPD</a> If I want the button to navigate to the next page when dragged, what code should I write? ...

"Unlocking the potential of AngularJS: A guide to accessing multiple controllers

I am trying to set a variable in one instance of a controller and read it in another. The variable I need to set is within the object vm (so $scope cannot be used). This is the code for the controller: app.controller("AppController", function(){ var ...

The functionality of HTML5 Camera is experiencing issues while being used with Tomcat7

My Angular2 project includes HTML5 camera access functionality. I launch the project using Angular CLI (ng serve), which starts the web container for testing purposes. When trying to access the camera, the browser prompts me for permission. Once granted, e ...

Difficulty in toggling on and off several form elements with JavaScript

Trying to control multiple form elements on an HTML page with JavaScript has presented a challenge for me. In my form, each row contains a checkbox that should enable/disable the elements on that row. The issue I'm facing is that only the first two f ...

Can file input buttons be custom styled?

Since I am using Material-UI, the traditional methods are not working for me. I have a form with two buttons positioned next to each other. One button is an "Upload File" input for users to upload a file, and the other is a submit button. I want both butto ...

Transform the data format received from the AJAX request - modify the input value

I have a data variable that contains an object structured as follows: { "details": { "id": 10, "name": John Doe, "hobbies": [{ "id": 1, "name": "Football" }, { "id": 2, "name": "Badminton" }, ...

Easily iterate through the <li> elements using jQuery and append them to the <datalist> dynamically

My jQuery loop seems to be malfunctioning as it's not showing the values of my li elements. Instead, I'm seeing [object HTMLElement] in my input search bar. <div id="sidebar-wrapper"> <input type="text" list="searchList" class="searc ...

Drop-down selection triggering horizontal auto-scroll

Within my div element, I have a table with dropdowns. The div is set up to be horizontally scrollable. To create the dropdown functionality, I utilized Harvesthq Chosen. Let me provide some context. In Image 1, you'll notice that I've scrolled ...

parallax scrolling can be a bit bumpy

While working on a website, I've incorporated a slight parallax effect that is functioning almost perfectly. However, I've noticed that the foreground divs tend to jump a little when scrolling down the page. At the top of the page, there is a di ...

Can you provide an example and explain the functions `getOptionSelected` and `getOptionLabel` in Material UI?

While going through the Mui Documentation, I came across the Autocomplete component section and found two interesting props: getOptionLabel and getOptionSelected. Although I read their definitions, I'm still struggling to grasp them fully. Can someone ...

Variable assigned by Ajax no longer accessible post-assignment

Recently, I've written a script to fetch data from a SQL database and now I'm in the process of creating a JS wrapper for it. Utilizing certain functions, I call the script and extract information from the DB as soon as it becomes available. var ...