2

Is there a way to get line number of a certain tag using JavaScript?

For example, if I have the following HTML file:

<!doctype html>
<html>
  <head>
    <title>test</title>
  </head>
  <body>
    <script>
      x = "<script>"
    </script>
    <script>
    </script>
  </body>
</html>

then:

  • head tag is on line 3
  • title tag is on line 4
  • body tag is on line 5
  • script tag is on line 7 and 10

Perhaps something like document.getElementsByTagName('script')[0].lineNumber?

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
Kokizzu
  • 24,974
  • 37
  • 137
  • 233
  • 1
    There are no simple&beautiful ways to do it via JavaScript/DOM, e.g. there are no methods/properties like you've suggested. But see [this question](http://stackoverflow.com/q/2044642/2170192) for possible solutions/workarounds. – Alex Shesterov Apr 30 '13 at 12:08

1 Answers1

2

Once the HTML has been through a DOM parser, any link to the source code is discarded.

You would have to refetch the HTML and then parse it yourself, keeping track of the line numbers as you go.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335