i know how to 开发者_如何学Godo it in php: but how would i do that in javascript.
thanks =)
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
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;
};
精彩评论