~/ equivalent in javascript

后端 未结 12 1983
萌比男神i
萌比男神i 2020-12-12 19:11

Any smart way of doing a \"root\" based path referencing in JavaScript, just the way we have ~/ in ASP.NET?

12条回答
  •  半阙折子戏
    2020-12-12 20:04

    Solution for ASP.NET MVC applications

    This works when using IIS and also IIS Express in VS.

    Put this snippet before all scripts load, in order to have the root url variable "approot".

    at your service in the scripts:

    
    
     --> other scripts go here or somewhere later in the page.
    

    Then use it in your script or page script. Example:

    var sound_root_path = approot + "sound/";
    var img_root_path = approot + "img/";
    

    the approot variable will be something either:

    "/YourWebsiteName/" <-- IIS

    or just:

    "/" <-- IIS Express

提交回复
热议问题