~/ equivalent in javascript

后端 未结 12 1986
萌比男神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:02

    Kamarey's answer can be improved to support a dynamic base path:

        
              
     
    

    This will ensure a correct root path regardless of deployment configuration.

    To be fair, this doesn't answer the original question, but it elimiates most needs for getting the root path from javascript. Simply use relative URL's everywhere, without prefixing with slash.

    Should you still need to access it from javascript, add an id attribute and use document.getElementFromId() as MiffTheFox suggested - but on the base-tag.

提交回复
热议问题