简体   繁体   English

如何在javascript中的网址中发送变量

[英]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. 我知道如何在php中进行操作:但是我将如何在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 我可能会将其作为搜索查询IE ?key=value ,然后可以使用window.location.search进行访问。

You may need to clarify your answer but if you had a url variable such as 您可能需要澄清答案,但是如果您有url变量,例如

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

then you can simply add your variable with myuri + variablename if your variable is a string. 那么您可以简单地使用myuri + variablename添加myuri + variablename如果您的变量是字符串)。 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 如果您希望通过查询字符串之类的方式在Javascript的URL中发送变量,则可以再次通过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. 尽管可以将PHP嵌入到页面中,但是您需要更改为事件驱动模型才能使Javascript工作。 The equivalent of your example in JavaScript would be something like 与JavaScript中的示例等效的是

<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): 然后在您的javascript文件中,您需要先确保已设置i ,然后单击链接,然后该函数才能按以下方式工作(在Javascript文件中):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM