Passing a var from javascript into a url on a web page

L

Lasse Reichstein Nielsen

Rainmanjam said:
I have a variable that is in my javascript code. I would like to
create a url with variables from javascript to pass to another page.

Example:

http://www.bla.com/test.aspx?var1="jsvar1"&var2="jsvar2" etc.

What would be the best way of doing this?

var url = "http://www.bla.com/test.aspx?var1=" + urlEscape(jsvar1)
+ "&var2=" + urlEscape(jsvar2);

I assume you'll be sending it to a server without telling it the
encoding, so you'll have to use the same encoding as normal form
submission: text/url-encoded.

The urlEscape function could be written as something like:

function urlEscape(string) {
return escape(string).replace(/\+/g,"%2B").replace(/%20|[ ]/g,"+");
}

(not guaranteed to work for all characters)

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top