在 javascript 中从应用程序根获取路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3655868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Take path from application root in javascript
提问by Vaibhav Jain
function detailed_link(cell_rowid) {
var $tabs = $('#infrTab').tabs();
$tabs.tabs('select', 1); // switch to third tab~
objRowData = $('#' + pageGridId).getRowData(cell_rowid);
//document.getElementById("Name").value = objRowData.amount;
loadPage('Infringement/TaskDetail', 'taskDetails'); /* Path */
}
I have write a javascript function loadPage(), that needs a path to some page as a parameter. I need to give this path from the application root. I dont want a relative path. Please let me know how can I do it.
我写了一个 javascript 函数 loadPage(),它需要一个指向某个页面的路径作为参数。我需要从应用程序根目录给出这个路径。我不想要相对路径。请让我知道我该怎么做。
回答by Alastair Pitts
I have this piece of Javascript in my Site.master, just underneath the jquery import and above any reference to my own scripts.
我在我的 Site.master 中有这段 Javascript,就在 jquery 导入的下面和对我自己的脚本的任何引用之上。
<script type="text/javascript">
//Function that provides a relative URL for cases of virtual directories.
jQuery.url = function (path) {
return '<%: Url.Action("Index","Home") %>' + path;
};
</script>
This assumes that your '/' address is handled by your Index method in your Home controller (the standard).
这假设您的“/”地址由您的 Home 控制器(标准)中的 Index 方法处理。
You can then access it via:
然后您可以通过以下方式访问它:
$.url('Infringement/TaskDetail')
回答by Amin Saqi
suppose that you have a page with this address: http://sub.domain.com/page.htm. use the following in page code to achive those results:
假设您有一个具有以下地址的页面:http://sub.domain.com/page.htm。在页面代码中使用以下内容来实现这些结果:
window.location.host: you'll getsub.domain.com:8080orsub.domain.com:80window.location.hostname: you'll getsub.domain.comwindow.location.protocol: you'll gethttp:window.location.port: you'll get8080or80window.location.origin: you'll gethttp://sub.domain.com
window.location.host: 你会得到sub.domain.com:8080或sub.domain.com:80window.location.hostname: 你会得到sub.domain.comwindow.location.protocol: 你会得到http:window.location.port: 你会得到8080或80window.location.origin: 你会得到http://sub.domain.com
回答by berushka
I recommend to use relative path, because you never know where will be application root. If your application will be installed with another applications in IIS, then its root could be for example http://www.domain.com/iis/youapp/pages/one.aspx
我建议使用相对路径,因为您永远不知道应用程序根目录在哪里。如果您的应用程序将与 IIS 中的其他应用程序一起安装,则其根目录可能是例如http://www.domain.com/iis/youapp/pages/one.aspx

