2015-08-18 59 views
0

我需要在Javascript(而不是当前URL)中找到请求的URL,以识别加载了两个显示的网站中的哪一个。在Javascript中获取请求的URL

场景:我目前正在为Icinga Web 2构建一个模块,它可以选择并排显示两个帧(两个单独的.phtml文件)。当两个这些帧的是开放的,在浏览器中显示的URL看起来如下:

http://host/icingaweb/mymodule/siteA#!/icingaweb/mymodule/siteB 

您可以通过点击一个按钮,分别刷新单帧。在浏览器日志(和Apache日志),你可以看到,对于每个负载只是特定的页面被请求:

// reloading siteA 
GET http://host/icingaweb/mymodule/siteA 
// reloading siteB 
GET http://host/icingaweb/mymodule/siteB 

我到底需要在Javascript这个单一的请求(至少是要求的网站)。

不幸的是,以下功能不起作用:

var pathname = window.location.pathname; 
// returns (no matter which site is reloaded): "/icingaweb/mymodule/siteA" 
var url  = window.location.href; 
// returns (no matter which site is reloaded): "http://host/icingaweb/mymodule/siteA#!/icingaweb/mymodule/siteB" 

的Icinga网2全解析JavaScript内容到一个单一的文件,该文件是继全局访问(icinga.min.js)。自定义JavaScript必须放在module.js中。

module.js

(function(Icinga) { 
    var mymodule = function(module) { 
     /** 
     * The Icinga.Module instance (public/js/Icinga/module.js) 
     */ 
     this.module = module; 
     this.initialize(); 
     this.module.icinga.logger.info('mymodule module loaded'); 
    }; 

    mymodule.prototype = { 
     initialize: function() 
     { 
      /** 
      * Tell Icinga about our event handlers, these are then 
      * automatically detached once Icinga is unloading 
      */ 
      this.module.on('rendered', this.showURLs); 
     }, 

     showURLs: function(event) 
     { 
      var pathname = window.location.pathname; 
      // returns (no matter which site is reloaded): "/icingaweb/module/siteA" 
      var url  = window.location.href; 
      // returns (no matter which site is reloaded): "http://host/icingaweb/module/siteA#!/icingaweb/module/siteB" 
     } 
    }; 

    /** 
    * Register this module so that Icinga knows about it 
    * 
    * It's important that the identifier used is the same 
    * as the name of the module itself (case sensitive) 
    */ 
    Icinga.availableModules.mymodule = mymodule; 
}(Icinga)); 
+0

您是否尝试过document.referrer? – Helio

+0

@Helio'document.referrer'是你从哪里来的背页的网址。 – Jai

+0

document.referrer返回一个空值。 – lumen

回答

0

对于那些谁是在溶液interessted,琼的Icinga的Web开发人员2回答我的问题在monitoring-portal.org

您可以使用以下方法来获得的URL当前加载的帧/页面:

$(event.target).closest('.container').data('icingaUrl') 

非常感谢JoNe。