Relative Paths in Javascript in an external file

JavaScript file paths

When in script, paths are relative to displayed page

to make things easier you can print out a simple js declaration like this and using this variable all across your scripts:

Solution, which was employed on StackOverflow around Feb 2010:

<script type="text/javascript">
   var imagePath="http://sstatic.net/so/img/";
</script>

If you were visiting this page around 2010 you could just have a look at StackOverflow’s html source, you could find this badass one-liner [formatted to 3 lines 🙂 ] in the <head /> section

Leave a Comment