如何恢复窗口,而不是打开一个新的窗口,如果它有相同的url(仍然从上一个打开)

How to restore the window instead of open a new one if it has the same url(and still opened from the last one )?

本文关键字:窗口 如果 上一个 url 恢复 何恢复 一个      更新时间:2023-09-26

我使用下面的类来弹出一个特定的窗口,但我想问如何使用同一个类,如果用户单击一个按钮并弹出具有特定URL的窗口,然后再次单击具有相同URL的按钮,我不想打开新窗口,只要仍然打开相同的窗口。

public static void Redirect(string url, string target, string windowFeatures)
        {
            HttpContext context = HttpContext.Current;
            if ((String.IsNullOrEmpty(target) ||
                target.Equals("_self", StringComparison.OrdinalIgnoreCase)) &&
                String.IsNullOrEmpty(windowFeatures))
            {
                context.Response.Redirect(url);
            }
            else
            {
                Page page = (Page)context.Handler;
                if (page == null)
                {
                    throw new InvalidOperationException("Cannot redirect to new window outside Page context.");
                }
                url = page.ResolveClientUrl(url);
                string script;
                if (!String.IsNullOrEmpty(windowFeatures))
                {
                    script = @"window.open(""{0}"", ""{1}"", ""{2}"");";
                }
                else
                {
                    script = @"window.open(""{0}"", ""{1}"");";
                }
                script = String.Format(script, url, target, windowFeatures);
                ScriptManager.RegisterStartupScript(page, typeof(Page), "Redirect", script, true);
            }
        }

window.open方法的第二个参数是目标窗口的名称。如果两个调用都使用相同的值,则第二个调用会将其页面加载到先前打开的窗口中。

window.open('./a.html', 'popup', ...);
window.open('./b.html', 'popup', ...);

只有一个窗口加载了页面b.html