bzupnick
bzupnick

Reputation: 2814

how do i send a variable in the url in javascript

i know how to do it in php: but how would i do that in javascript.

thanks =)

Upvotes: 1

Views: 1562

Answers (2)

acconrad
acconrad

Reputation: 3209

You may need to clarify your answer but if you had a url variable such as

var myuri = 'http://www.stackoverflow.com'

then you can simply add your variable with myuri + variablename if your variable is a string. Concatenating is very easy.

If you are looking to send a variable in the URL in Javascript through something like a query string, again you can simply control that with appending string parameters onto the string of the page's location via window.location

EDIT: Now that I've seen your example, you have to realize that you're changing your paradigm from server-side to client-side programming language. Whereas PHP can be embedded into a page, you'll need to change to an event-driven model for Javascript to work. The equivalent of your example in JavaScript would be something like

<a href="#" onclick="addVariable(i);">Hello world</a>

and then in your javascript file you would need to make sure that i is set before you click the link, and then the function would work as follows (in a Javascript file):

function addVariable(queryStringVariable)
{
    window.location = "http://www.someurl.com/page?" + queryStringVariable;
};

Upvotes: 0

Robert
Robert

Reputation: 21388

Your answer is going to depend heavily on your other requirements. I'd probably send it as a search query, IE ?key=value, and then you can access that with window.location.search

Upvotes: 3

Related Questions