如何将 jQuery 函数放入我的代码后面?

发布于 2024-12-23 12:05:36 字数 5312 浏览 3 评论 0原文

我有下面的 jQuery 函数 (Show_popupWindows):

    $(function () {
                        var popupWindows_Done = $('input[type="hidden"]#hfpopupWindows_Done').val();
                        if (popupWindows_Done == "false") {
                            Show_popupWindows();
                        }
                        else {
                        }
    });    //End Of $(function ()
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
                function Show_popupWindows() {
                    var x = screen.availWidth;
                    //alert(x);
                    $('div#OffDiv').css({ 'width': x });
                    //alert($('div#OffDivAndLoadingContainer').css('width'));
                    var y = screen.availHeight;
                    //alert(y);
                    $('div#OffDiv').css({ 'height': y });
                    //alert($('div#OffDivAndLoadingContainer').css('height'));

                    //alert('On');
                    $('div#OffDiv').css({ 'display': 'block' });
                    $('div#TimerContainer').css({ 'display': 'block' });

                    var w1 = window.open('http://www.blablabla.com', '_blank', 'channelmode=no,directories=yes,location=no,resizable=yes,titlebar=yes,menubar=no,toolbar=no,scrollbars=yes,status=yes', false);
                    var w2 = window.open('http://www.blablabla.com', '_blank', 'channelmode=no,directories=yes,location=no,resizable=yes,titlebar=yes,menubar=no,toolbar=no,scrollbars=yes,status=yes', false);
                    window.focus();

                    var sec = $('#TimerContainer span').text()
                    var timer = setInterval(function () {
                        $('#TimerContainer span').text(--sec);
                        if (sec == 0) {
                            clearInterval(timer);
                            $('div#OffDiv').css({ 'display': 'none' });
                            $('div#TimerContainer').css({ 'display': 'none' });
                        }
                    }, 1000);

                    var watchClose = setInterval(function () {
                        try {
                            if (w1.closed || w2.closed) {
                                clearTimeout(watchClose);
                                clearInterval(timer);
                                DoNotClosePoPupWindowDuringTimer();
                            }
                        }
                        catch (e) {
                        }
                    }, 200);
                }
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

如何将此函数放入我的代码后面,这样就没有人可以通过编辑 aspx 文件来更改此函数。

我在代码后面尝试了下面的方法:

private static StringBuilder Create_ShowpopupWindows_Function()
{
    StringBuilder sb = new StringBuilder();
    sb.Append("var x = screen.availWidth;");
    sb.Append("//alert(x);");
    sb.Append("$('div#OffDiv').css({ 'width': x });");
    sb.Append("//alert($('div#OffDivAndLoadingContainer').css('width'));");
    sb.Append("var y = screen.availHeight;");
    sb.Append("//alert(y);");
    sb.Append("$('div#OffDiv').css({ 'height': y });");
    sb.Append("//alert($('div#OffDivAndLoadingContainer').css('height'));");

    sb.Append("//alert('On');");
    sb.Append("$('div#OffDiv').css({ 'display': 'block' });");
    sb.Append("$('div#TimerContainer').css({ 'display': 'block' });");

    sb.Append("var w1 = window.open('http://www.almasafzar.com', '_blank', 'channelmode=no,directories=yes,location=no,resizable=yes,titlebar=yes,menubar=no,toolbar=no,scrollbars=yes,status=yes', false);");
    sb.Append("var w2 = window.open('http://www.soscharge.com', '_blank', 'channelmode=no,directories=yes,location=no,resizable=yes,titlebar=yes,menubar=no,toolbar=no,scrollbars=yes,status=yes', false);");
    sb.Append("window.focus();");

    sb.Append("var sec = $('#TimerContainer span').text()");
    sb.Append("var timer = setInterval(function () {");
    sb.Append("$('#TimerContainer span').text(--sec);");
    sb.Append("if (sec == 0) {");
    sb.Append("clearInterval(timer);");
    sb.Append("$('div#OffDiv').css({ 'display': 'none' });");
    sb.Append("$('div#TimerContainer').css({ 'display': 'none' });");
    sb.Append("}");
    sb.Append("}, 1000);");

    sb.Append("var watchClose = setInterval(function () {");
    sb.Append("try {");
    sb.Append("if (w1.closed || w2.closed) {");
    sb.Append("clearTimeout(watchClose);");
    sb.Append("clearInterval(timer);");
    sb.Append("DoNotClosePoPupWindowDuringTimer();");
    sb.Append("}");
    sb.Append("}");
    sb.Append("catch (e) {");
    sb.Append("}");
    sb.Append("}, 200);");
    return sb;
}

但是当我在 page_load 中调用它时它不起作用:

        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder sb = Create_ShowpopupWindows_Function();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Show_popupWindows", sb.ToString(), true);
        }

I have the jQuery function below (Show_popupWindows) :

    $(function () {
                        var popupWindows_Done = $('input[type="hidden"]#hfpopupWindows_Done').val();
                        if (popupWindows_Done == "false") {
                            Show_popupWindows();
                        }
                        else {
                        }
    });    //End Of $(function ()
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
                function Show_popupWindows() {
                    var x = screen.availWidth;
                    //alert(x);
                    $('div#OffDiv').css({ 'width': x });
                    //alert($('div#OffDivAndLoadingContainer').css('width'));
                    var y = screen.availHeight;
                    //alert(y);
                    $('div#OffDiv').css({ 'height': y });
                    //alert($('div#OffDivAndLoadingContainer').css('height'));

                    //alert('On');
                    $('div#OffDiv').css({ 'display': 'block' });
                    $('div#TimerContainer').css({ 'display': 'block' });

                    var w1 = window.open('http://www.blablabla.com', '_blank', 'channelmode=no,directories=yes,location=no,resizable=yes,titlebar=yes,menubar=no,toolbar=no,scrollbars=yes,status=yes', false);
                    var w2 = window.open('http://www.blablabla.com', '_blank', 'channelmode=no,directories=yes,location=no,resizable=yes,titlebar=yes,menubar=no,toolbar=no,scrollbars=yes,status=yes', false);
                    window.focus();

                    var sec = $('#TimerContainer span').text()
                    var timer = setInterval(function () {
                        $('#TimerContainer span').text(--sec);
                        if (sec == 0) {
                            clearInterval(timer);
                            $('div#OffDiv').css({ 'display': 'none' });
                            $('div#TimerContainer').css({ 'display': 'none' });
                        }
                    }, 1000);

                    var watchClose = setInterval(function () {
                        try {
                            if (w1.closed || w2.closed) {
                                clearTimeout(watchClose);
                                clearInterval(timer);
                                DoNotClosePoPupWindowDuringTimer();
                            }
                        }
                        catch (e) {
                        }
                    }, 200);
                }
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

how can I put this function into my code behind, so nobody can change this function by editing aspx file.

I tried the method below in code behind :

private static StringBuilder Create_ShowpopupWindows_Function()
{
    StringBuilder sb = new StringBuilder();
    sb.Append("var x = screen.availWidth;");
    sb.Append("//alert(x);");
    sb.Append("$('div#OffDiv').css({ 'width': x });");
    sb.Append("//alert($('div#OffDivAndLoadingContainer').css('width'));");
    sb.Append("var y = screen.availHeight;");
    sb.Append("//alert(y);");
    sb.Append("$('div#OffDiv').css({ 'height': y });");
    sb.Append("//alert($('div#OffDivAndLoadingContainer').css('height'));");

    sb.Append("//alert('On');");
    sb.Append("$('div#OffDiv').css({ 'display': 'block' });");
    sb.Append("$('div#TimerContainer').css({ 'display': 'block' });");

    sb.Append("var w1 = window.open('http://www.almasafzar.com', '_blank', 'channelmode=no,directories=yes,location=no,resizable=yes,titlebar=yes,menubar=no,toolbar=no,scrollbars=yes,status=yes', false);");
    sb.Append("var w2 = window.open('http://www.soscharge.com', '_blank', 'channelmode=no,directories=yes,location=no,resizable=yes,titlebar=yes,menubar=no,toolbar=no,scrollbars=yes,status=yes', false);");
    sb.Append("window.focus();");

    sb.Append("var sec = $('#TimerContainer span').text()");
    sb.Append("var timer = setInterval(function () {");
    sb.Append("$('#TimerContainer span').text(--sec);");
    sb.Append("if (sec == 0) {");
    sb.Append("clearInterval(timer);");
    sb.Append("$('div#OffDiv').css({ 'display': 'none' });");
    sb.Append("$('div#TimerContainer').css({ 'display': 'none' });");
    sb.Append("}");
    sb.Append("}, 1000);");

    sb.Append("var watchClose = setInterval(function () {");
    sb.Append("try {");
    sb.Append("if (w1.closed || w2.closed) {");
    sb.Append("clearTimeout(watchClose);");
    sb.Append("clearInterval(timer);");
    sb.Append("DoNotClosePoPupWindowDuringTimer();");
    sb.Append("}");
    sb.Append("}");
    sb.Append("catch (e) {");
    sb.Append("}");
    sb.Append("}, 200);");
    return sb;
}

but it does not work when I call it in page_load :

        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder sb = Create_ShowpopupWindows_Function();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Show_popupWindows", sb.ToString(), true);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回忆追雨的时光 2024-12-30 12:05:36

这是客户端代码,那么如何防止用户更改它呢?这是不可能的。

It's clientside code, so how are you going to prevent the user from being able to change it? It's not possible.

那一片橙海, 2024-12-30 12:05:36

您可以将脚本文件嵌入到程序集文件中,而不是在代码隐藏中使用 StringBuilder 创建脚本以避免客户(从您对问题的评论中获取)操作脚本。

  1. 右键单击脚本并选择 buid 操作:嵌入资源
  2. 将脚本位置添加到程序集信息文件中:
    [程序集:System.Web.UI.WebResource("SomeFolder.myJavascript.js", "text/js")]
  3. 在页面上包含脚本:

    字符串脚本位置 =
    Page.ClientScript.GetWebResourceUrl(this.GetType(), "SomeFolder.myJavascript.js");
    Page.ClientScript.RegisterClientScriptInclude("SomeFolder.myJavascript.js", scriptLocation);

Instead of creating the script using StringBuilder in code behind to avoid the customer (taking that from your comments on your question) to manipulate the script you can embed the script file in the assembly file.

  1. Right click on the script and selected buid action: Embedded Resource.
  2. Add the script location to the assembly info file:
    [assembly: System.Web.UI.WebResource("SomeFolder.myJavascript.js", "text/js")]
  3. Include the script on the page:

    string scriptLocation =
    Page.ClientScript.GetWebResourceUrl(this.GetType(), "SomeFolder.myJavascript.js");
    Page.ClientScript.RegisterClientScriptInclude("SomeFolder.myJavascript.js", scriptLocation);

听你说爱我 2024-12-30 12:05:36

您的页面运行的任何 JavaScript 对您的浏览器以及任何愿意右键单击并查看源代码的人都是可见的。尝试找到一些棘手的方法来让 ASP.NET 注入脚本并不能改变这一点。

如果您担心有人弄乱脚本并导致问题,那么您必须重新审视如何在服务器端验证事物。

Any JavaScript that your page runs will be visible to your browser, and anyone who cares to right-click and view source. Trying to find tricky ways to get asp.net to inject the script cannot change that.

If you're worried about someone messing with the script and causing problems, then you have to take a fresh look at how you're validating things on the server side.

小鸟爱天空丶 2024-12-30 12:05:36

jQuery 基于 Javascript 代码,通常(在本例中)在客户端计算机上执行。这意味着您无法阻止用户更改代码。

您可以采取一些措施来阻止它们,例如混淆和阻止它们。压缩它: 如何混淆 JS 代码

更强大的解决方案是控制它是什么你试图从服务器端进行保护。

jQuery is based on Javascript code, which is generally(and in this case) executed on the clients machine. This means there is no way you can prevent a user from changing the code.

There are some things you can do to deter them, like obfuscating & compressing it: How to obfuscate JS code

The more robust solution is to control whatever it is you're trying to protect from the server side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文