ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Javascript window.open

Javascript window.open

原创 Linux操作系统 作者:abstractcyj 时间:2013-02-28 13:57:43 0 删除 编辑
最近项目当中遇到了一个javascript浏览器的兼容问题
使用window.open打开新的窗口。以下是代码:
 
function openMenuUrl(url, windowId){
  if(windowId){
    windowId  = windowId + cs2_generateTempId(windowId);
    var scalar = 0.9;
    var w_ht = Math.round(scalar * screen_ht);
    var w_wd = Math.round(scalar * screen_wd);
    var w_left = Math.round((screen_wd-w_wd)*0.5);
    var w_top = Math.round((screen_ht-w_ht)*0.5);
    windowparms = "resizable=yes,status=yes,scrollbars=yes,";
    windowparms += "left=" + w_left + ",top=" + w_top + ",screenX=" + w_left + ",screenY=" + w_top + ",";
    windowparms += "height=" + w_ht + ",width=" + w_wd + ",toolbar=no";

    if (isStandardZoneFunction(url)){
      window.open("../transition.htm", windowId, windowparms);
  } else if(url.indexOf("cs1MocToMcc=true") == -1){
      window.open("../progress.htm", windowId, windowparms);
  }

  if((myNewWin = window.open(url, windowId, windowparms)) == null ) {
    var_proceed=true;
    return;
  }
  myNewWin.focus();
  var_proceed=true;
  } else{
    window.location.href = url;
  }
}

期望行为是能在新窗口当中先看到transition.htm的内容,然后再跳转到传入的url。
但是FF/Chrome中,不能看到transition.htm的内容。
原因:

https://developer.mozilla.org/en-US/docs/DOM/window.open

The open() method creates a new secondary browser window, similar to choosing New Window from the File menu. The strUrl parameter specifies the URL to be fetched and loaded in the new window. If strUrl is an empty string, then a new blank, empty window (URL about:blank) is created with the default toolbars of the main window.

Note that remote URLs won't load immediately. When window.open() returns, the window always contains about:blank. The actual fetching of the URL is deferred and starts after the current script. block finishes executing. The window creation and the loading of the referenced resource are done asynchronously.

解决方法:


function openMenuUrl(url, windowId){
  if(windowId){
    windowId  = windowId + cs2_generateTempId(windowId);
    var scalar = 0.9;
    var w_ht = Math.round(scalar * screen_ht);
    var w_wd = Math.round(scalar * screen_wd);
    var w_left = Math.round((screen_wd-w_wd)*0.5);
    var w_top = Math.round((screen_ht-w_ht)*0.5);
    windowparms = "resizable=yes,status=yes,scrollbars=yes,";
    windowparms += "left=" + w_left + ",top=" + w_top + ",screenX=" + w_left + ",screenY=" + w_top + ",";
    windowparms += "height=" + w_ht + ",width=" + w_wd + ",toolbar=no";

    if (isStandardZoneFunction(url)){
      window.open("../transition.htm", windowId, windowparms);
  } else if(url.indexOf("cs1MocToMcc=true") == -1){
      window.open("../progress.htm", windowId, windowparms);
  }
  setTimeout(function() {
      if((myNewWin=window.open(url,windowId,windowparms))==null) {
       return;
      }
      myNewWin.focus();
    }, 1000);

  var_proceed=true;
  } else{
    window.location.href = url;
  }
}

此处用到了setTimeout与闭包。





来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8520577/viewspace-754948/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Web应用程序优化
下一篇: Oracle Model Clause
请登录后发表评论 登录
全部评论
曾从事java方向开发多年。近年已经转入数据库方向。主要擅长SQL优化,Oracle数据库问题诊断,Oracle备份与恢复等。服务于医药物流,医院等行业

注册时间:2010-01-26

  • 博文量
    602
  • 访问量
    1091603