我有这个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>


我试图从我的代码后面调用此函数,如下所示:

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


全部想法是在我的网站上有弹出窗口,以防万一或出现错误消息。
VB代码采用了我想要的所有属性。
但是我的aspx.page中的函数没有被调用。

最佳答案

请尝试以下一项:

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

关于javascript - 从aspx中的代码后面调用JavaScript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20478848/

10-12 00:04
查看更多