Get url to external script


/ Published in: JavaScript
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.