The Javascript function is experiencing a failure to be invoked from the backend code

In my JavaScript function, I have the following code:

function CloseWindow() {
    alert("I am firing");    // window.close();         
}

I attempted to call this function from the code behind button click event using the following code snippet:

Page.ClientScript.RegisterStartupScript([GetType](), "Javascript", "javascript:CloseWindow();", True)

However, the function does not display the alert message as expected. Interestingly, when I call the same function from OnClientClick, it works properly.

OnClientClick="javascript:();"

What could be causing this issue? Please feel free to ask for further clarification if needed.

Contributed by

<script type="text/javascript" language="javascript">
        function CloseWindow() {
            alert("I am firing");
//            window.close();<br>
        }
        function chkLength(evt, len) {
            var str = document.getElementById(evt.id);
            if (str.value.length < len)
                return true;
            else
                return false;
        }
    </script>

Answer №1

When utilizing Update Panels, you have the option to use the following:

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "javascriptFunction();", true);

If not using Update Panels, you can instead utilize:

ClientScript.RegisterStartupScript
        (GetType(),Guid.NewGuid().ToString(), "javascriptFunction();",true);

Answer №2

give this code a shot....

ClientScript.RegisterStartupScript
        (GetType(),Guid.NewGuid().ToString(), "javascript: CloseWindow();",true);

Answer №3

Make sure that the third argument (script) is set to "CloseWindow();" instead of using "javascript:CloseWindow();"

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 procedure for accessing controls on the Site.Master page?

I have a Site.Master page along with folders containing individual "projects" that each have their own Master page and detail page: ~/Site.Master myProject/Project1.Master myProject/Project1.aspx myProject/Project1.cs In the Project1 ...

Next.js fails to refresh the content upon initial view

Snippet from my index.js file: import Post from "@/components/Post" import Modal from "@/components/Modal" import {useState} from "react" export default function Home() { // Setting up states const [modalTitle, setModalTitle] = useState('Title&a ...

Emphasize a specific string of text within a Word Document

I am currently developing a project on an ASP.NET Website using Web Forms and .NET 4.0. I have a word document that was created using Aspose.Words and now I need to implement a function to highlight specific strings in the document. The function should wor ...

The `modelName` model cannot be recompiled as it has already been written

Issue with Updating partnerCode Model After Compilation. I have a file called models/partnerCode.js var mongoose = require('mongoose'); var Schema = mongoose.Schema; var partnerCodeSchema = new Schema({ email: String, used: {type: Numb ...

In Node.js, callbacks do not stop the code execution when they are called

During the development of my node.js application, I encountered a perplexing issue - sometimes the callback doesn't stop the execution as expected and it continues running. Approach 1 utilityMethod(arg1, arg2, arg2,function(err,result){ if(er ...

Using the ES6 object spread operator in the JSX syntax of ReactJS

// modules/NavLink.js import React from 'react' import { Link } from 'react-router' export default React.createClass({ render() { //for example this.props={from: "home", to: "about"} return <Link {...this.props} a ...

Managing Angular Directives Through Controllers

My Angular directive functions as a login popup, opening a popup page when triggered. modal = angular.module('Directive.Modal',[]); modal.directive('modalLogin',function() { return { restrict: 'EA', scope ...

Guide to displaying pages on the dashboard in a React application with the help of Material UI

I am currently facing a slight difficulty in managing my dashboard using material-UI along with other components. The workflow of the application includes opening the login form first and then navigating to the dashboard. My goal is to only change the righ ...

What is the best way to utilize the constructor in a JavaScript object so that only the properties within `this` are utilized?

I am looking to instantiate the following class: class Person { firstName; lastName; birthday; constructor(props: Person) { {firstName, lastName, birthday} = props } } var me = new Person({firstName: "donald", lastName: "trum ...

Determining the height of dynamically rendered child elements in a React application

Looking for a way to dynamically adjust the heights of elements based on other element heights? Struggling with getting references to the "source" objects without ending up in an infinite loop? Here's what I've attempted so far. TimelineData cons ...

Why are the class variables in my Angular service not being stored properly in the injected class?

When I console.log ("My ID is:") in the constructor, it prints out the correct ID generated by the server. However, in getServerNotificationToken() function, this.userID is returned as 'undefined' to the server and also prints as such. I am puzz ...

Ways to replicate this impact

I came across a perfect match for what I need; it resembles a relationship user interface. Please check it out: How can I replicate that? I am looking to develop a PHP application that can retrieve relationships from a database and showcase them similarly ...

sum inside the while loop

Below is the provided HTML: <form> <textarea id="input1"></textarea> <textarea id="input2"></textarea> <span></span> </form> The following JavaScript code is included: $("#input2").keyup{ var a = ...

quickest method for attaching a click listener to dynamically added elements in the document object model

When adding multiple elements to the DOM, how can we ensure that these elements also have a click handler attached to them? For instance, if there is a click handler on all elements with the "canvas-section" class, and new "canvas-section" elements are con ...

Troubleshooting axios GET request in JavaScript: despite successfully pushing data to an array, encountering errors when using promise

I'm attempting to send a GET request to a free API in order to retrieve an array of all French departments. Initially, I encountered an issue where I was getting an empty result, which I later figured out was due to not waiting for the GET request to ...

Is there a way to insert a divider within the enquirer.js multiselect prompt for a Yeoman generator?

I am currently working on a Yeoman generator project and have encountered an issue with adding a separator within a multi-select choice set using enquirer.js. In my attempt to solve this, I explored the possibility of utilizing the following package: http ...

Adding a cell to a specific position within a table row using JQuery and the target position in the row

In my table, I have the following structure: https://i.sstatic.net/oKvBD.png The status in the thead has a unique numerical value, and the date also has a distinct date value. What I need is to add a cell after matching the (status cell and date cell), ...

What is the best way to find an onmouseover element with Selenium in Python?

I've been attempting to scrape a website and encountered an element that reveals information in a bubble when the mouse hovers over it. I am using Selenium for web scraping, but I am unsure how to locate this specific element. After examining the pag ...

Adjusting the empty image source in Vue.js that was generated dynamically

Currently experimenting with Vue.js and integrating a 3rd party API. Successfully fetched the JSON data and displayed it on my html, but encountering issues with missing images. As some images are absent from the JSON file, I've saved them locally on ...

What is the best way to increment the value of an input by any number using JavaScript and HTML?

My code is causing a NaN error, and I'm struggling to identify the root cause. Even providing a specific number in the addnum function did not resolve the issue. <script> var result = document.getElementById("result"); var inputVal = ...