1

I have this javascript:

<script language="javascript" type="text/javascript">
    function NewWindow(mypage, myname, w, h, scroll, pos) {
        var win = null;
        if (pos == "random") {
            LeftPosition = (screen.width) ? Math.floor(Math.random() * (screen.width - w)) : 100;
            TopPosition = (screen.height) ? Math.floor(Math.random() * ((screen.height - h) - 75)) : 100;
        }
        if (pos == "center") {
            LeftPosition = (screen.width) ? (screen.width - w) / 2 : 100;
            TopPosition = (screen.height) ? (screen.height - h) / 2 : 100;
        }
        else if ((pos != "center" && pos != "random") || pos == null) {
            LeftPosition = 0; TopPosition = 20
        }
        settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
        win = window.open(mypage, myname, settings);
    }
</script>

And I'm trying to call this function from my code behind as follows:

Dim Message As String = "This is My new Message"
Dim url As String = HttpContext.Current.Request.Url.AbsoluteUri
Dim script As String = "NewWindow(" & url & "," & Message & ",200,50,Yes,random)"
        If Not Page.ClientScript.IsStartupScriptRegistered(Me.GetType(), "alertscript") Then
            Page.ClientScript.RegisterStartupScript(Me.GetType(), "alertscript", script, True)
        End If

The all idea is to have popup windows in case or error messages in my web site.
The vb code is taking all the attributes I want.
But the function in my aspx.page is not called.

14
  • Where in the code are you calling RegisterStartupScript from? Commented Dec 9, 2013 at 19:26
  • Are you using JQuery? If yes, maybe you can set with your code behind "$(document).ready()" to run you alert calling after entire document is available. Try a simple alert first, and them in you function put a simple alert and call the function to verify if it is firing. Commented Dec 9, 2013 at 19:33
  • @SystemDown the script I call it from my code behind and in the Load Event. Commented Dec 9, 2013 at 19:57
  • @AndrewPaes Please give more assistance on your suggestion because I'm not so familiar with script and asp net. This my first work. Commented Dec 9, 2013 at 19:59
  • Have you turned on the script console in your browser? A simple JavaScript error somewhere could be causing this. Commented Dec 9, 2013 at 20:00

2 Answers 2

1

Try this one please:

Default.aspx

<%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
    CodeBehind="Default.aspx.vb" Inherits="TesteVB._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <script language="javascript" type="text/javascript">
        function Test() {
            alert('Banzai');
        }

        function NewWindow(mypage, myname, w, h, scroll, pos) {
            var win = null;
            if (pos == "random") {
                LeftPosition = (screen.width) ? Math.floor(Math.random() * (screen.width - w)) : 100;
                TopPosition = (screen.height) ? Math.floor(Math.random() * ((screen.height - h) - 75)) : 100;
            }

            if (pos == "center") {
                LeftPosition = (screen.width) ? (screen.width - w) / 2 : 100;
                TopPosition = (screen.height) ? (screen.height - h) / 2 : 100;
            }
            else if ((pos != "center" && pos != "random") || pos == null) {
                LeftPosition = 0; TopPosition = 20
            }

            settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';

            win = window.open(mypage, myname, settings);
        }
    </script>
</asp:Content>

Default.aspx.vb

Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim csname1 As String = "PopupScript"
        Dim csname2 As String = "AutoPopup"
        Dim cstype As Type = Me.GetType()

        ' Get a ClientScriptManager reference from the Page class.
        Dim cs As ClientScriptManager = Page.ClientScript

        ' Check to see if the startup script is already registered.
        If (Not cs.IsStartupScriptRegistered(cstype, csname1)) Then

            'Dim cstext1 As String = "Test();"
            'cs.RegisterStartupScript(cstype, csname1, cstext1, True)

            Dim Message As String = "This is My new Message"
            Dim url As String = HttpContext.Current.Request.Url.AbsoluteUri
            Dim script As String = "NewWindow('" & url & "','" & Message & "',200,50,'Yes','random');"

            cs.RegisterStartupScript(cstype, csname1, script, True)

        End If
    End Sub

End Class
Sign up to request clarification or add additional context in comments.

10 Comments

I did paste the code and the new script in the exact place I run it but I have the same result... Nothing... I have a secrete filling that the script never call it... for some reason...
I tested here and everything functioned normally with the same code. Pay attention that I put the quotation marks. I tested in Firefox with my debug on and gave no error. Uncomment the lines that makes the call the Test function and comment on others that are below and tell me what happened. Your Web browser is enabled to perform JS?
Yes by default it is the ie 11. I have Google as well... same issues
Do you have another JS script that might be giving error in your code?
No all the other scripts i was use are run just fine
|
0

I think you want to place "NewWindow where your script is.. like this..

Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "NewWindow();", true);

This is how I call mine.

Edit: Try this. It's not tested.

Page.ClientScript.RegisterStartupScript(Me.[GetType](), "Call my function", "NewWindow();", True)

5 Comments

I think I've miss a ; is that important?
What I have when I use the Inspect Element in my browser is { } that is the script... completely empty from parameters...
Is it possible to run the script from inside of my code behind? and if I can how can I do it?
Do you want to declare a JS function in the aspx page and then perform the function of the code behind? No you can't do that. When you declare a JS function it will be a client side script and can only be performed by client events while/after the page is loaded. You can make a declaration of a JS function from code behind and call a JS function from the code behind, but it can only be executed from a client-side event.
@Humpy I did the changes and nothing happen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.