Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1015083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:20:55+00:00 2026-05-16T10:20:55+00:00

How can a javascript file know where it is located? For example: <script type=text/javascript

  • 0

How can a javascript file know where it is located? For example:

<script type="text/javascript" src="http://mysite.com/scripts/howdy.js"></script>

How can the code in howdy.js know about http://mysite.com/scripts/howdy.js?

Edit: Clarification. I cannot rely on searching the DOM for my script tag because I need to know about it before the DOM may be ready.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:20:56+00:00Added an answer on May 16, 2026 at 10:20 am

    In the moment in which the current script is being executed, will be the last script element in the DOM, therefore you can get it by:

    var scripts = document.getElementsByTagName('script'),
        currentScriptSrc = scripts[scripts.length-1].src;
    

    Check this example that loads this script.

    Edit: Taking in consideration the @kangax’s comment, about the async and defer attributes, the only safe way IMO, previously knowing the file name, would be to inspect the script elements on the page, examining its src attribute, some libraries like Scriptaculous.us use this technique, for example:

    var scripts = document.getElementsByTagName('script'),
        len = scripts.length,
        re = /howdy\.js$/,
        src, howdyScriptSrc;
    
    while (len--) {
      src = scripts[len].src;
      if (src && src.match(re)) {
        howdyScriptSrc = src;
        break;
      }
    }
    ​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that I can run an external Javascript file from within HTML with
I know that you can use a php file as an external javascript file
What happens to comments in a minified JavaScript file? How can the browser know
How can javascript know file upload progress? Is there some kind of standard communication
I want to use a JavaScript file, lying in the root folder. How can
Can JQuery/JavaScript read/get the file content on the client side, where the file is
Javascript can manipulate the document the browser is displaying, so the following: <script> document.write(<table><tr><td>Hola</td><td>Adios</td></tr></table>);
Can someone show me how to include this javascript file into my wordpress plugin.
How can I get a Javascript file to minity like this? eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return
I have some code that downloads a file from the internet located here: http://www.amsat.org/amsat/ftp/keps/current/nasa.all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.