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

  • Home
  • SEARCH
  • 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 6195983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:33:53+00:00 2026-05-24T03:33:53+00:00

Possible Duplicate: How may I reference the script tag that loaded the currently-executing script?

  • 0

Possible Duplicate:
How may I reference the script tag that loaded the currently-executing script?

Is there a way to select the script element that included a particular script without giving it a known attribute of any sort?

This example will alert the window object because it’s being called in the global context:

<script>
  alert(this);
</script>

This example wont work as the name of the script could change (for convenience i’m assuming jQuery is included):

<script type="text/javascript" src="/foo/bar/baz.js"></script>
//baz.js
$('[src^="baz.js"]');

This example is sub-optimal as it relies on the id attribute:

<script type="text/javascript" id="foo">
  $('#foo');
</script>
  • 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-24T03:33:54+00:00Added an answer on May 24, 2026 at 3:33 am

    I’m hesitant to answer my own question in this case because I don’t have an answer that works 100% of the time, but I’ve ironed out the bugs enough to have a stable solution.

    @Shef’s answer is halfway to what I wanted/needed.

    Generic JS Version:

    function getActiveScript()
    {
      var s;
      s=document.getElementsByTagName('script');
      return s[s.length - 1];
    }
    

    The issue with the above code is that it will select the last script on the page no matter when it was executed. This is a major issue if the calling script was included after page load.

    As I can’t directly select the script element (unless another solution presents itself), I’d rather not select anything if the script is being added after page load.

    Pure JS Answer:

    function getActiveScript()
    {
      var r,s;
      r = document.readyState;
      if ( r && r != 'complete' )
      {
        s = document.getElementsByTagName('script');
        return s.length ? s[s.length - 1] : null;
      }
      return;
    }
    

    In the new version, document.readyState is checked to make sure that the document hasn’t finished loading. Additionally on browsers where document.readyState isn’t used, it will fail out (I can’t think of any browsers that don’t use document.readyState these days).

    One caveat to this solution is that it may be possible to dynamically insert a script element into an earlier part of the document, which then gets executed before document.readyState is 'complete'. If the script were to use this code, it could possibly be referencing the wrong script. I haven’t checked this issue yet. Typically I add new script tags with document.body.appendChild (or equivalent in a library), so it’s not that big an issue to me.

    Another possible loophole is if a new script has been appended to document.body before getActiveScript has been called. Again, I haven’t tested it, but it may mean that the last selected script evaluates to on that’s different than the one being executed.


    Actual Usage:

    The question was asked of why I wanted to select the currently evaluating script element. Although “Because I can” would probably be understood, I did have a legitimate reason to want to use this feature/functionality/dirty-nasty-hack.

    I created a (couple) jQuery plugin(s) to do what I originally wanted.

    jQuery Plugin: jquery.activescript.js

    (function($){
      "use strict";
      $.activeScript = function(){
        var r;
        r=document.readyState;
        return r && r != 'complete' ? $('script:last') : $();
      };
    })(jQuery);
    

    jQuery Plugin: jquery.activescript.plugin.js

    by default works for jQuery UI

    (function($){
      "use strict";
      var plugins,d,p,$p;
      d = $.activeScript().data();
      plugins = d.plugins || 'draggable droppable resizable selectable sortable accordion autocomplete button datepicker dialog progressbar slider tabs';
      plugins=plugins.split(' ');
      $(function(){
        while(p=plugins.pop())
        {
          $p=$(d[p+'Selector']);
          if ($p[p])$p[p]();
        }
      });
    })(jQuery);
    

    The way you’d use jquery.activescript.plugin.js is as follows:

    <script type="text/javascript" src="jquery.activescript.plugin.js"
      data-draggable-selector=".draggable.default"
      data-droppable-selector=".droppable.default"
      data-resizable-selector=".resizable.default"
      data-selectable-selector=".selectable.default"
      data-sortable-selector=".sortable.default"
      data-accordion-selector=".accordion.default"
      data-autocomplete-selector=".autocomplete.default"
      data-button-selector=".button.default"
      data-datepicker-selector=".datepicker.default"
      data-dialog-selector=".dialog.default"
      data-progressbar-selector=".progressbar.default"
      data-slider-selector=".slider.default"
      data-tabs-selector=".tabs.default"></script>
    

    This would allow you to semantically tie in your default plugins to your page without having to muck about in a JS file. If non-defaults are needed, you should muck about in a JS file, so I haven’t added any mechanism for specifying options (although a few plugins could really use it).

    This same basic activescript mechanism could be used by standalone plugins to have an easier way of overriding defaults.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How may I reference the script tag that loaded the currently-executing script?
Possible Duplicate: C++ variable types limits I have a defined type that may not
Possible Duplicate: Is there a CSS parent selector? Hi! I'm trying to select a
Possible Duplicate: Collection was modified; enumeration operation may not execute HI there, I am
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicates: When pass-by-pointer is preferred to pass-by-reference in C++? Are there benefits of
Possible Duplicate: What's an actual use of variable variables? OK, this question may look
Possible Duplicate: Are there are any platforms where pointers to different types have different
Possible Duplicate: Jquery event chaining Traditionally we may write: $(selector).click(function () { }); but
Possible Duplicate: How to run a database script file from Delphi? I have a

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.