2015-08-18 57 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。