首页 > 解决方案 > 从类文件调用 JavaScript

问题描述

我尝试从浏览器发送通知。当我使用 aspx 页面中的代码时,它运行良好,但是当我将它放入类文件中的函数时,它不起作用。

这是我的代码:

public static void browserNotification(string title, string desc, string url)
    {
        string code = "";
        code = @" { 
            if (Notification.permission !== 'granted') { 
                Notification.requestPermission(); 
            }
        });

        function customnotify(" + title + ", " + desc + ", " + url + @") {

            if (Notification.permission !== 'granted') {
                Notification.requestPermission();
            }
            else {
                var notification = new Notification(title, {
                    icon: 'http://SilenceMusicSchool.com/image/logo_icon.jpg',
                    body: desc,
                });

                /* Remove the notification from Notification Center when clicked.*/
                notification.onclick = function () {
                    window.open(url);
                };

                /* Callback function when the notification is closed. */
                notification.onclose = function () {
                    console.log('Notification closed');
                };
            }
        }";
        customnotify(" + title + ", " + desc + ", " + url + ");";
        System.Web.HttpContext.Current.Response.Write("<script type='text/javascript'>" + code + "</script>");
    }

我想在 aspx.cs 中调用函数:

Classes.browserNotification("a title", "a body", "url");

标签: javascriptc#asp.netclassnotifications

解决方案


推荐阅读