We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

micmath on 06/16/08


Tagged


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

korzhik


Get url to external script


Published in: JavaScript 


This could be included inside the external script itself, and compared to window.location if the returned URL were relative.

  1. function getPathToSelf() {
  2. var myName = /(^|[\/\\])path\.js(\?|$)/;
  3. var scripts = document.getElementsByTagName("script");
  4. for (var i = 0; i < scripts.length; i++) {
  5. var src;
  6. if (src = scripts[i].getAttribute("src")) {
  7. if (src.match(myName)) {
  8. return src;
  9. }
  10. }
  11. }
  12. }
  13. alert(getPathToSelf());

Report this snippet 

You need to login to post a comment.