IrishChieftain
IrishChieftain

Reputation: 15253

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. 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. 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.

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>

Upvotes: 0

Views: 1005

Answers (2)

Pointy
Pointy

Reputation: 413709

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").

Upvotes: 1

William
William

Reputation: 1467

From this post: Get URL of ASP.Net Page in code-behind

You can use: HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) to get the host name, i.e. root url, with the HTTP:// at the beginning.

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.

Upvotes: 1

Related Questions