do not assume that the browser won't add new script tags to the DOM before it's done with the evaluation of OpenLayers.js, r=fredj,ahocevar (closes #2873)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10862 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Éric Lemoine
2010-10-27 10:26:05 +00:00
parent 3c57b6b36c
commit 8024e58177
4 changed files with 45 additions and 7 deletions

View File

@@ -44,10 +44,19 @@
* {String} Path to this script
*/
_getScriptLocation: (function() {
var r = new RegExp("(^|(.*?\\/))(" + scriptName + ")(\\?|$)");
var s = document.getElementsByTagName('script');
var m = s[s.length-1].getAttribute("src").match(r);
var l = m ? m[1] : "";
var r = new RegExp("(^|(.*?\\/))(" + scriptName + ")(\\?|$)"),
s = document.getElementsByTagName('script'),
src, m, l = "";
for(var i=0, len=s.length; i<len; i++) {
src = s[i].getAttribute('src');
if(src) {
var m = src.match(r);
if(m) {
l = m[1];
break;
}
}
}
return (function() { return l; });
})()
};

View File

@@ -25,9 +25,19 @@ var OpenLayers = {
* {String} Path to this script
*/
_getScriptLocation: (function() {
var s = document.getElementsByTagName('script');
var m = s[s.length-1].getAttribute("src").match(/(^|(.*?\/))(OpenLayers\.js)(\?|$)/);
var l = m ? m[1] : "";
var r = new RegExp("(^|(.*?\\/))(OpenLayers\.js)(\\?|$)"),
s = document.getElementsByTagName('script'),
src, m, l = "";
for(var i=0, len=s.length; i<len; i++) {
src = s[i].getAttribute('src');
if(src) {
var m = src.match(r);
if(m) {
l = m[1];
break;
}
}
}
return (function() { return l; });
})()
};

18
tests/OpenLayers4.html Normal file
View File

@@ -0,0 +1,18 @@
<html>
<head>
<script type="text/javascript">
OpenLayers = {singleFile: true}; // just to make the test run faster
document.write('<scr'+'ipt src="../lib/OpenLayers.js"></scr'+'ipt>');
document.write('<scr'+'ipt src="bogus/foo-/OpenLayers.js"></scr'+'ipt>');
</script>
<script type="text/javascript">
function test_OpenLayers(t) {
t.plan(1);
t.eq(OpenLayers._getScriptLocation(), "../lib/",
"Script location correctly detected, and not fooled by other scripts.");
}
</script>
</head>
<body>
</body>
</html>

View File

@@ -159,6 +159,7 @@
<li>OpenLayers1.html</li>
<li>OpenLayers2.html</li>
<li>OpenLayers3.html</li>
<li>OpenLayers4.html</li>
<li>Popup.html</li>
<li>Popup/Anchored.html</li>
<li>Popup/AnchoredBubble.html</li>