Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having an ampersand in a querystring parameter

I'm a bit new to javascript and I'm having a minor problem:

I'm trying to redirect to a page (which then performs a redirect) in javascript. I'm setting the window.location like so:

window.location = "./RedirectPage.aspx?ReturnUrl=page.aspx?key=val&key2=val2";

Now, on RedirectPage.aspx when it's trying to redirect to the page that I passed in as the ReturnUrl, it is parsing key2=val2 as being another querystring parameter for RedirectPage instead of the ReturnUrl.

It makes sense that it does that, but that's not what I am trying to do... any idea how I might solve this?

like image 488
Steven Evers Avatar asked Apr 30 '12 15:04

Steven Evers


People also ask

How do you pass ampersand in URL parameter?

For example, to encode a URL with an ampersand character, use %26. However, in HTML, use either & or &, both of which would write out the ampersand in the HTML page.

What characters are allowed in query string?

The query component is a string of information to be interpreted by the resource. Within a query component, the characters ";", "/", "?", ":", "@", "&", "=", "+", ",", and "$" are reserved.

What is %27 in query string?

The %27 is ASCII for the single quote ( ' ) and that is a red flag for someone trying to perform SQL injection via the query string to your application's data access layer logic.

How do you escape ampersand in HTML?

the escape sequence is *another* ampersand. In the case of backslashes, the escape sequence is *another* backslash.


2 Answers

You want to URL encode the ReturnUrl querystring.

window.location = "./RedirectPage.aspx?ReturnUrl="+encodeURIComponent("page.aspx?key=val&key2=val2");
like image 86
Theron Luhn Avatar answered Sep 21 '22 08:09

Theron Luhn


Try this:

window.location = "./RedirectPage.aspx?"+encodeURIComponent("ReturnUrl=page.aspx?key=val&key2=val2")

You need to escape the ampersand (for use in a query string).

like image 32
Rocket Hazmat Avatar answered Sep 18 '22 08:09

Rocket Hazmat