From 74a9468bb97053350697825eb9939e3d3f45b9fc Mon Sep 17 00:00:00 2001
From: juanf <juanf>
Date: Thu, 2 Mar 2017 13:14:15 +0000
Subject: [PATCH] SSDM-4841 : bugfix, finding correct base url even with
 scripts loaded from a different domain

SVN: 37853
---
 .../cisd/openbis/public/resources/config.js        | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/config.js b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/config.js
index 5c639fb9d29..d07541fdce8 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/config.js
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/config.js
@@ -1,9 +1,17 @@
 var require = (function() {
 
 	var getBaseUrl = function() {
-		var path = window.location.pathname;
-		var index = path.substr(1).indexOf("/") + 1;
-		var baseUrl = path.substring(0, index) + "/resources";
+		// To find where the V3 API has been loaded from we scan the script tags until finding this file
+		// This way is possible to load the V3 API from a separate domain
+		var scripts = document.getElementsByTagName("script");
+		var baseUrl = null;
+		for(var sIdx = 0; sIdx < scripts.length; sIdx++) {
+			var src = scripts[sIdx].src;
+			var substringEnd = -1;
+			if((substringEnd = src.indexOf("/api/v3/config.js")) > -1) {
+				baseUrl = src.substring(0, substringEnd+"/api/v3".length);
+			}
+		}
 		return baseUrl;
 	}
 
-- 
GitLab