最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how can you determine location of <script> tag from inside said tag? - Stack Overflow

programmeradmin0浏览0评论

I am trying to figure out the location of the script tag the current javascript is running in. What is really going on is that I need to determine from inside a src'd, dynamically inserted javascript file where it is located in the DOM. These are dynamically generated tags; code snippet:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>where am i?</title>
  <script type="text/javascript" charset="utf-8">
    function byId(id) {
      return document.getElementById(id);
    }

    function create_script(el, code) {
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.text = code;
      el.appendChild(script);
    }
  </script>
</head>
<body>
  <div id="find_me_please"></div>
  <script>
    create_script(byId("find_me_please"), "alert('where is this code located?');");
  </script>
</body>
</html>

I am trying to figure out the location of the script tag the current javascript is running in. What is really going on is that I need to determine from inside a src'd, dynamically inserted javascript file where it is located in the DOM. These are dynamically generated tags; code snippet:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>where am i?</title>
  <script type="text/javascript" charset="utf-8">
    function byId(id) {
      return document.getElementById(id);
    }

    function create_script(el, code) {
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.text = code;
      el.appendChild(script);
    }
  </script>
</head>
<body>
  <div id="find_me_please"></div>
  <script>
    create_script(byId("find_me_please"), "alert('where is this code located?');");
  </script>
</body>
</html>
Share Improve this question edited Aug 26, 2016 at 17:10 carefulnow1 8411 gold badge13 silver badges31 bronze badges asked Dec 30, 2008 at 20:49 Matt SecoskeMatt Secoske 411 gold badge2 silver badges5 bronze badges 1
  • did you figure this out? I'm struggling with the same issue... – ekkis Commented Nov 23, 2011 at 22:16
Add a ment  | 

3 Answers 3

Reset to default 5

You could give the script an id tag, like this dude does...

You can use document.write to create a dummy DOM object and use parentNode to escape out. For example:

<script>
(function(r) {
  document.write('<span id="'+r+'"></span>');
  window.setTimeout(function() {
    var here_i_am = document.getElementById(r).parentNode;
    ... continue processing here ...
  });
})('id_' + (Math.random()+'').replace('.','_'));
</script>

This assumes you don't actually have control of the <script> tag itself, such as when it's inside a <script src="where_am_i.js"></script> - if you do have control of the <script> tag, simply put an ID on it, as in:

<script id="here_i_am">...</script>

If you are just running this on page load, this works

<script>
  var allScripts = document.getElementsByTagName('script');
  var thisScript = allScripts[allScripts.length];
  alert(thisScript);
</script>
发布评论

评论列表(0)

  1. 暂无评论