How to get relative path in Javascript?

前端 未结 6 684
野的像风
野的像风 2020-12-09 18:47

In my ASP.net web project, I\'ve written the following Javascript code in a .js file:

function getDeviceTypes() {
    var deviceTypes;
    $.ajax({
        a         


        
6条回答
  •  一生所求
    2020-12-09 19:05

    You've got two options:

    1. Build a configuration/ preferences object in JavaScript which contains all your environment specific settings:

       var config = {
           base: <% /* however the hell you output stuff in ASPX */ %>,
           someOtherPref: 4
       };
      

      and then prefix the AJAX url with config.base (and change the value for config.base whether you're on a dev/ testing/ deployment server.)

    2. Use the HTML tag to set the URL prefix for all relative URL's. This affects all relative URL's: image's, links etc.

    Personally, I'd go for option 1. You'll most likely find that config object coming in handy elsewhere.

    Obviously the config object will have to be included in a part of your site where server-side-code is evaluated; a .js file won't cut it without configuring your server. I always include the config object in the HTML ; its a small config object, whose contents can change on each page, so it's perfectly warrented to stick it in there.

提交回复
热议问题