Reputation: 4047
With this code I will get the full path of the executed script, in this case services.js
:
JS
var scripts = document.getElementsByTagName("script"),
src = scripts[scripts.length-1].src;
alert(src);
Output example:
file:///C:/Users/fel/Desktop/currencies-gh-pages/js/services.js
But i want an output like this:
file:///C:/Users/fel/Desktop/currencies-gh-pages/js/
Any idea?
Upvotes: 2
Views: 1642
Reputation: 43728
"regular expression to remove the file name from the full url"
You could replace all the chars that arent /
at the end of the string using the following regex /[^\/]+$/
.
src.replace(/[^\/]+$/, '');
However, it seems to be the slower solution compared to some other solutions: http://jsperf.com/replace-speed-test
Upvotes: 1
Reputation: 3236
You could use http://phpjs.org/functions/dirname/
var txt = 'file:///C:/Users/fel/Desktop/currencies-gh-pages/js/services.js';
function dirname (path) {
// http://kevin.vanzonneveld.net
// + original by: Ozh
// + improved by: XoraX (http://www.xorax.info)
// * example 1: dirname('/etc/passwd');
// * returns 1: '/etc'
// * example 2: dirname('c:/Temp/x');
// * returns 2: 'c:/Temp'
// * example 3: dirname('/dir/test/');
// * returns 3: '/dir'
return path.replace(/\\/g, '/').replace(/\/[^\/]*\/?$/, '');
}
alert(dirname(txt));
Upvotes: 1
Reputation: 19040
I think you could split it instead.
src = src.split('/');
src.pop();
alert(src.join('/'));
Upvotes: 3