Javascript 中的应用程序根目录
我将尽可能多的 javascript 放在 MyApp.js 中,这样我就不必在 ASPX 页面中寻找标签。但是,当开发目录和部署目录不同时,我的应用程序根目录会出现问题,因此我使用全局变量(appRoot)来手动更改部署和开发时间之间的路径。我尝试了 window.location.url、window.location.host 等,但没有任何效果。因为我无法使用 <%: Url.Content("~/AppRoot") %>在 .js 中,如何使 .js 中引用的任何路径独立于我的部署目录所在的位置?感谢您的帮助。
var appRoot = "/2_1/"; //deployment path
//var appRoot = "/"; //development path
$(function () {
$("#txtSSNPage1,#txtSSNPage2").blur(function () {
if ($(this).val() != undefined && jQuery.trim($(this).val()).length != 0) {
try {
var form = $(this).parents('form:first');
form.attr('action', appRoot + 'Controller1/SSN/' + escape(jQuery.trim($(this).val())));
form.submit();
}
catch (err) {
alert(err.description);
}
} //if
}); //blur
$("input#txtNamePage3").blur(function () {
if ($(this).val() != undefined && jQuery.trim($(this).val()).length != 0) {
try {
var form = $(this).parents('form:first');
form.attr('action', appRoot + 'Controller2/SSN/' + escape(jQuery.trim($(this).val())));
form.submit();
} catch (err) {
alert(err.description);
}
} //if
}); //blur
$("input#txtNamePage4").blur(function () {
if ($(this).val() != undefined && jQuery.trim($(this).val()).length != 0) {
try {
var form = $(this).parents('form:first');
form.attr('action', appRoot + 'Controller2/FullName/' + escape(jQuery.trim($(this).val())));
form.submit();
} catch (err) {
alert(err.description);
}
} //if
}); //blur
});
I put as much as my javascript in the MyApp.js so that I don't have to hunt for tag among ASPX pages. However, I have problem with my app root when the development directory and the deployment directory are different, so I use a global variable (appRoot) to manually change the path between deployment and development time. I tried window.location.url, window.location.host, etc, nothing worked. Since I can not use <%: Url.Content("~/AppRoot") %> in .js, how can I make any path that is referenced in the .js independent on where my deployment directory is? Thank you for your help.
var appRoot = "/2_1/"; //deployment path
//var appRoot = "/"; //development path
$(function () {
$("#txtSSNPage1,#txtSSNPage2").blur(function () {
if ($(this).val() != undefined && jQuery.trim($(this).val()).length != 0) {
try {
var form = $(this).parents('form:first');
form.attr('action', appRoot + 'Controller1/SSN/' + escape(jQuery.trim($(this).val())));
form.submit();
}
catch (err) {
alert(err.description);
}
} //if
}); //blur
$("input#txtNamePage3").blur(function () {
if ($(this).val() != undefined && jQuery.trim($(this).val()).length != 0) {
try {
var form = $(this).parents('form:first');
form.attr('action', appRoot + 'Controller2/SSN/' + escape(jQuery.trim($(this).val())));
form.submit();
} catch (err) {
alert(err.description);
}
} //if
}); //blur
$("input#txtNamePage4").blur(function () {
if ($(this).val() != undefined && jQuery.trim($(this).val()).length != 0) {
try {
var form = $(this).parents('form:first');
form.attr('action', appRoot + 'Controller2/FullName/' + escape(jQuery.trim($(this).val())));
form.submit();
} catch (err) {
alert(err.description);
}
} //if
}); //blur
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用(.aspx)
您可以在应用程序的动态部分调用 .js 文件之前
you can use
before calling the .js file in the dinamic part of your app (.aspx)
如果每个页面上始终包含一个脚本,您可以使用其位置来识别站点根目录:
如果您将该脚本存储在 Scripts 子目录或类似目录中,请根据需要调整第二行。
If there's a script that's always included on every page you can use its location to identify the site root:
Adjust the second line as needed if you store the script in a Scripts subdirectory or similar.