~/ equivalent in javascript

后端 未结 12 1988
萌比男神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 19:55

    The following function will calculate the root of the currently running application. I use it to locate the absolute location of resources, when called from somewhere deep within the application tree.

        function AppRoot() {
            //
            // Returns the root of the currently running ASP application.
            // in the form: "http://localhost/TRMS40/"
            //
            //   origin: "http://localhost"
            // pathname: "/TRMS40/Test/Test%20EMA.aspx"
            //
            // usage:
            //           window.open( AppRoot() + "CertPlan_Editor.aspx?ID=" + ID);
            //
    
            var z = window.location.pathname.split('/');
    
            return window.location.origin + "/" + z[1] + "/";
        }
    

提交回复
热议问题