2

I have a script tag that is being loaded as a data uri, ie

<script type="text/javascript" src=[some data uri]></script>

I would like to be able to define something regarding my script such that when an error occurs on my page, rather than that error appearing in the console with a crazy data uri as the file name, it appears with a file name that I define.

Ideally the correct file name would also appear when you console.log.

Is this possible? Thanks!

asutherland
  • 2,849
  • 4
  • 32
  • 50
  • 2
    Why use a data URI for ` – Brad Aug 17 '13 at 15:40
  • Have a look here: http://stackoverflow.com/questions/464359/custom-exceptions-in-javascript – Darm Aug 17 '13 at 15:42
  • @Darm This doesn't really do what I need I don't think. Some other script might call a function in the script tag I am importing so I'd have to wrap every single function in a try catch or something, unless I misunderstand your suggestion. – asutherland Aug 17 '13 at 15:44
  • @Brad Because I have a bunch of script tags that I am creating dynamically based on text I am getting via postMessage. If I make them all inline then when errors hit the console they all appear to be coming from the HTML file which isn't useful. – asutherland Aug 17 '13 at 15:47
  • Sounds like you should be using a server-side language instead – Alex W Aug 17 '13 at 15:49
  • @asutherland I don't see how that is a problem, and it certainly isn't any better using data URIs. – Brad Aug 17 '13 at 17:48
  • @brad the sourceURL solution below works in either case. its a problem because in some browsers inline script error line numbers are relative to the top of the script tag, not to the top of the html file so if you have a bunch of script tags inline in the same html file it is impossible to find the error. – asutherland Aug 17 '13 at 18:21

1 Answers1

2

Turns out that you can do this in webkit with the //@ sourceURL directive at the bottom of your js code.

This even works in evaled code!

Pretty sweet: http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/

You can do a lot more with sourceMaps and there is support from other browsers, see here: http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-howwork

asutherland
  • 2,849
  • 4
  • 32
  • 50