简体   繁体   English

根据用户交互在页面焦点上调用Javascript函数

[英]Calling a Javascript function on focus of a page based on user interaction

I have a requirement where clicking on an icon should open a new window where the user will be able to view and edit certain fields. 我有一个要求,即单击图标应打开一个新窗口,用户可以在其中查看和编辑某些字段。 After the user closes this window and comes back to parent window, the icon color and text should be changed( for eg:- if the user has removed certain data, the icon will change to red color and text is set to null. If the user presses cancel button, nothing changes) 用户关闭此窗口并返回到父窗口后,应更改图标的颜色和文本(例如:-如果用户已删除某些数据,则图标将变为红色,并且文本设置为null。用户按下“取消”按钮,没有任何变化)

I am planning to implement this using a body onload function which essentially checks with the database using AJAX requests to see if the user has changed the data, then accordingly change the icon and text. 我打算使用body onload函数实现此功能,该函数实质上是使用AJAX请求与数据库进行检查,以查看用户是否更改了数据,然后相应地更改了图标和文本。

But, I see 2 problems in this approach 但是,我发现这种方法有两个问题
1. There will be a AJAX call even if the user has not changed anything.(ie. pressed Cancel button) 1.即使用户未进行任何更改,也将有一个AJAX呼叫。(即,按“取消”按钮)
2. AJAX is called every time the body is on focus. 2.每次身体聚焦时都会调用AJAX。 Eg:- He may be working on some other page (or a different browser altogether) and comes back to this, resulting in an AJAX call. 例如:-他可能正在其他页面(或完全不同的浏览器)上工作,并返回到此页面,从而导致AJAX调用。

Can anybody suggest a better approach. 任何人都可以提出更好的方法。 I am using Javacript, JSP, Java 我正在使用Javacript,JSP,Java

I think the easiest way to do this would be to set a cookie (learn how here ). 我认为最简单的方法是设置一个cookie( 在此处了解如何)。 You can then have the two windows communicate between each other. 然后,您可以使两个窗口相互通信。 This wouldn't be AJAX, but it will most likely work. 这不是AJAX,但很可能会起作用。

Two ways to implement this 两种实现方法

Method 1 方法1

You know the methods which changes the database in the opened form. 您知道在打开的表单中更改数据库的方法。 Suppose you have a delete method, write an additional window.opener.location.reload() after the method. 假设您有一个delete方法,请在该方法之后编写一个额外的window.opener.location.reload() The downside is that opener(parent window) gets reloaded every time you change something in the child window. 缺点是每次您在子窗口中进行更改时,打开器(父窗口)都会重新加载。 Which is unnecessary. 这是不必要的。

Method 2 - Using cookies 方法2-使用Cookie

I am using MDN's A little framework: a complete cookies reader/writer with full unicode support for creating cookies. 我正在使用MDN的A小框架:完整的cookie读取器/写入器,并具有用于创建cookie的完整unicode支持 The plan of action will be this. 行动计划将是这样。 Create a cookie and set a value for it like this after you change anything in the child window and update it in the database like this docCookies.setItem("isChildFormUpdated", "yes"); 在子窗口中更改任何内容并在数据库中像docCookies.setItem("isChildFormUpdated", "yes");这样更新后,创建一个cookie并为此设置一个值docCookies.setItem("isChildFormUpdated", "yes"); . You can use the same cookie for every action you do. 您可以对执行的每个操作使用相同的cookie。 Now when you navigate back to the parent form, do this. 现在,当您导航回父窗体时,请执行此操作。

$(document).ready() {
    $(window).focus(function () {
        var formCookie = docCookies.getItem("isChildFormUpdated");
        if (formCookie !== null && formCookie == "yes") {
            //resetting the cookie. you can also remove the cookie
            docCookies.setItem("isChildFormUpdated", "no");
            //docCookies.removeItem("isChildFormUpdated");
            // your ajax call comes here
            //or you could simply reload the form so that we get fresh data
            //window.location.reload(); // it will be heavier
        }
    });
});

I hope you get the basic idea. 希望您能掌握基本的想法。

Another nice way to create a popup-like box is by using a modal box. 创建类似弹出框的另一种不错的方法是使用模式框。 These can be complicated but they look very nice. 这些可能很复杂,但看起来非常不错。 You have to make a jQuery plugin in, but you can take the one here and learn how it works. 您必须在其中制作一个jQuery插件,但是您可以在此处使用jQuery插件并了解其工作方式。 Good luck with your requirement. 祝您好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM