简体   繁体   English

在JavaScript中设置Root的路径

[英]Set Path to Root in JavaScript

I'm doing a quick port of an ASP.NET Web Application Project from 1.1 to 2.0. 我正在快速将ASP.NET Web应用程序项目从1.1移植到2.0。 So, instead of a master page the template was created as a custom control. 因此,模板是作为自定义控件创建的,而不是母版页。 There is a search box in the side panel which is accessible from every page on the site. 侧面板上有一个搜索框,可从网站上的每个页面访问。 The panel contains two text boxes for first and second names. 该面板包含两个用于名字和名字的文本框。

On submitting the search params as query strings, the user is transferred to mysite.com/search/results.aspx. 在将搜索参数作为查询字符串提交时,用户将转移到mysite.com/search/results.aspx。 The problem is that depending on where the user submits the search from the path may come up as follows: 问题是,根据用户从路径提交搜索的位置,可能会出现如下情况:

mysite.com**/dir1/dir2**/search/results.aspx

I need to resolve it from the root and it looks like the JS location object is the problem. 我需要从根解析它,看起来JS位置对象是问题。

Here is the original code. 这是原始代码。 How can I construct the link to resolve from the root? 如何构建从根解析的链接?

rightGutter.Controls.Add(new LiteralControl("<script language=javascript>"));
rightGutter.Controls.Add(new LiteralControl
    ("function doPhoneSearch(txtval,txtVal1) {"));
rightGutter.Controls.Add
    (new LiteralControl("location.replace
        ('search/results.aspx?lnamedpco=' + txtval+'&fname='+txtVal1);"));
rightGutter.Controls.Add(new LiteralControl("txtval=\"\";"));
rightGutter.Controls.Add(new LiteralControl("return false;"));
rightGutter.Controls.Add(new LiteralControl("}"));
rightGutter.Controls.Add(new LiteralControl("</script>"));


HtmlTableCell rightCell8 = new HtmlTableCell();
rightCell8.Attributes.Add("align", "right");
rightCell8.Controls.Add
    (new LiteralControl
        ("<a onClick=\"doPhoneSearch(document.getElementsByName
            ('lnamedpco')[0].value,
                document.getElementsByName('fname')[0].value)\">"));
Image bgImage5 = new Image();
bgImage5.ImageUrl = "~/images/gobtn.gif";
bgImage5.Attributes.Add("runat", "server");
rightCell8.Controls.Add(bgImage5);
rightCell8.Controls.Add(new LiteralControl("</a>"));   


<a onClick=\"doPhoneSearch(document.getElementsByName('lnamedpco')[0].value,
    document.getElementsByName('fname')[0].value)\">
        <img SRC=\"http://mysite/images/gobtn.gif\" 
             BORDER=\"0\" ALT=\"Submit Form\">
</a>

From this post: Get URL of ASP.Net Page in code-behind 从这篇文章: 在代码隐藏中获取ASP.Net页面的URL

You can use: HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) to get the host name, ie root url, with the HTTP:// at the beginning. 您可以使用:HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)来获取主机名,即根URL,其开头是HTTP://。

So what I would do is replace this line: 所以我要做的就是替换这一行:

bgImage5.ImageUrl = "~/images/gobtn.gif";

With

bgImage5.ImageUrl = String.Format("{0}{1}",HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority),"/images/gobtn.gif");

That will give you an absolute URL as opposed to a root relative path but it should work. 这将为您提供绝对URL而不是根相对路径,但它应该工作。

Just start the path with a "/": 只需用“/”开始路径:

rightGutter.Controls.Add
    (new LiteralControl("location.replace
        ('/search/results.aspx?lnamedpco=' + txtval+'&fname='+txtVal1);"));

If you use "//" instead then it'll also pick up the right protocol string ("http" or "https"). 如果您使用“ //”代替,那么它还将选择正确的协议字符串(“ http”或“ https”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM