Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can you determine location of <script> tag from inside said tag?

Tags:

javascript

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>
like image 337
Matt Secoske Avatar asked Mar 01 '23 03:03

Matt Secoske


1 Answers

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

like image 118
sammich Avatar answered Mar 09 '23 00:03

sammich