获取数据的URL(如PHP的$ _GET)的jQuery / AJAX? [英] Getting data from URL (like php's $_GET) for jQuery/Ajax?
问题描述
什么我想要做的就是采取类似myurl.com/page.php?data1=x&data2=y&data3=z一个网址,让每一块数据,并用它在一个jQuery功能。是否有任何内置的方式做到这一点?
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?
要澄清一下,当我浏览到(例如,粘贴网址到地址栏,或者通过书签将它)我想jQuery的做一些与该数据。
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.
推荐答案
一个快速的方法来做到这一点是如果你的JavaScript code嵌入到一个PHP文件(外部链接到一个.js文件),你可能只是做一个PHP打印就在你的℃之间;脚本>< / SCRIPT>
标签。这将与书签和粘贴的URL地址栏。
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.
例如:
<script type="text/javascript">
var get_var;
get_var = <? print $_GET['varname']; ?>;
// some jQuery code here
</script>
这应该只被用来作为一个概念证明。这是容易的跨站脚本攻击的,并且应该在生产中不使用。对XSS prevention技术,这列表是良好的开端(而OP的问题的范围之内)。
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).
这篇关于获取数据的URL(如PHP的$ _GET)的jQuery / AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!