How to avoid hard-coding the application context path in js/ajax scripts

In the HTML page that includes the script you could put an HTML base tag that points to the context. See the answer to How to get domain URL and application name?

And you can read about the base tag at http://www.w3schools.com/tags/tag_base.asp which states The <base> tag specifies the base URL/target for all relative URLs in a document.

Before deciding whether or not to use this tag, it may be worth reading answers to Is it recommended to use the <base> html tag?


You can use $.ajaxPrefilter() to prepend context path to all jQuery AJAX requests.

It can be configured in <script> element of your pages, where context path value is available (e.g. ${pageContext.request.contextPath} in JSP).