从URL(如php的$ _GET)获取jQuery / Ajax的数据?
What I'm wanting to do is take a URL like myurl.com/page.php?data1=x&data2=y&data3=z and get each piece of data and use it in a jQuery function. Is there any built in way to do that?
To clarify, when I navigate to (say, pasting the url into the address bar, or going to it via a bookmark) I want jQuery to do something with that data.
As a bonus, if it also includes something to check if the data is set to something, that would be great.
我想要做的是使用像myurl.com/page.php?data1=x&这样的网址。 data2 = y& data3 = z并获取每个数据并在jQuery函数中使用它。 有没有内置的方法呢? p>
澄清一下,当我导航到(比如将网址粘贴到地址栏中,或通过书签转到它)时,我想要jQuery 用这些数据做点什么。 p>
作为奖励,如果它还包括检查数据是否设置为某物的东西,那就太棒了。 p> DIV>
A quick way to do it would be if your javascript code is embedded in a php file (not an external link to a .js file), you could just do a PHP print right in between your <script></script>
tags. It would work with bookmarks and pasting the URL to the address bar.
For example:
<script type="text/javascript">
var get_var;
get_var = <? print $_GET['varname']; ?>;
// some jQuery code here
</script>
This should only be used as a proof of concept. This is vulnerable to XSS attacks, and should not be used in production. This list of XSS prevention techniques is a good place to start (but outside the scope of the OP's question).
You're looking for how to get values from the query string. Here's pretty much the same question: