1

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

thanks =)

3
  • The same way: you append it to the URL. Commented Apr 22, 2011 at 2:40
  • show us how you would do it in PHP and someone might suggest a Javascript solution. Commented Apr 22, 2011 at 2:41
  • in php i would just do <a href = 'index.php?id=$i">link</a> Commented Apr 22, 2011 at 2:43

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.