开发者

App Root in Javascript

开发者 https://www.devze.com 2023-03-26 12:57 出处:网络
I put as much as my javascript in the MyApp.js so that I don\'t have to hunt fortag among ASPX pages.However, I have problem with my app root when the development directory and the deployment director

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 trie开发者_如何学运维d 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

});


you can use

<% response.write "<script>var appRoot =  """ & Url.Content("~/AppRoot") & """</script>" %>

before calling the .js file in the dinamic part of your app (.aspx)


If there's a script that's always included on every page you can use its location to identify the site root:

var myScript = $('script[src$="myscript.js"]');
var baseUrl = myScript.attr('src').substring(0, myScript.attr('src').length - "myscript.js".length);

Adjust the second line as needed if you store the script in a Scripts subdirectory or similar.

0

精彩评论

暂无评论...
验证码 换一张
取 消