【问题标题】:Cannot set title of page loaded through Window.Open无法设置通过 Window.Open 加载的页面标题
【发布时间】:2013-09-06 22:02:23
【问题描述】:

我正在使用下面的代码显示 Silverlight 中的页面

public void OpenLinkInNewWindow(string url, int? width = null, int? height = null)
{
    try
    {
        Argument.IsNotNull("url", url);

        if (!width.HasValue)
        {
          width = int.Parse(HtmlPage.Window.Eval("screen.width").ToString()) - MarginLeft;
        }

      if (!height.HasValue)
      {
        // Subtract the margin twice because of the start menu (which we assume is on the bottom)
        height = int.Parse(HtmlPage.Window.Eval("screen.height").ToString()) - MarginTop - (MarginTop / 2);
      }

      string sizeJavascript = string.Empty;
      sizeJavascript += string.Format(",width={0}", width.Value);
      sizeJavascript += string.Format(",height={0}", height.Value);

      int leftOffset = MarginLeft / 2;
      int topOffset = MarginTop / 2;

      string javascript = string.Format("window.open('{0}', '', 'left={1},top={2},scrollbars=1,resizable=1,modal=no,alwaysRaised=yes{3}')", url, 
                leftOffset, topOffset, sizeJavascript);

      HtmlPage.Window.Eval(javascript);
   }
   catch (Exception)
   {
     //There may be a problem loading the window via Javascript so try to open in new page
     try
     {
       OpenLinkNewPage(url);
     }
     catch (Exception)
     {
        //There is still a problem - perhaps a popup blocker.  So as a last resort, open link in the same page
        OpenLinkSamePage(url);
     }
   }
}

public void OpenLinkSamePage(string url)
{
  HtmlPage.Window.Navigate(new Uri(url), string.Empty);
}

public void OpenLinkNewPage(string url)
{
   HtmlPage.Window.Navigate(new Uri(url), "_blank");
}
}

这在加载新窗口时效果很好

但是,我这辈子都不知道如何设置这个窗口的标题?

我尝试了很多不同的方法,但它们都不起作用

我已经尝试过 PageLoad,在 aspx 中使用脚本标签

我猜这可能是安全限制?

我看到有人建议将 window.open 的结果放到一个变量中,然后设置标题,但这只会导致页面显示在新标签中

我的页面标记中确实有报告,但这被忽略了

我需要将其显示在新窗口而不是新标签中

我使用的是 IE 10

有人有什么想法吗?

保罗

【问题讨论】:

    标签: c# javascript html


    【解决方案1】:

    您是否尝试过这种方法:

    var javascript = string.Format("var win = window.open(); win.document.title('{0}'); win.focus();", "success!");
    

    ?

    【讨论】:

    • 刚刚试过了,还是不行,我只是得到一个异常“Eval failed”?
    • 嗨,蒂姆,是的,我做到了。我也尝试只选择包含类型的程序集,但它仍然不起作用
    • 现在变得更加陌生了!在我的机器上这不起作用,但在另一台也有 IE 10 的机器上,标题已正确更改并显示只读标题栏?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多