我正在尝试将查询字符串从URL传递到Javascript函数,但我所做的似乎没有任何工作。 我正在使用PHP来生成Javascript代码。
如果我有以下网址:
www.testing.com/?test=5
我希望该函数输出以下代码:
"googletag.pubads().setTargeting("test","5");', 'close');"
我尝试使用 echo $_GET["test"]
添加 PHP 代码,但它不是显示值,而是显示$_GET["test"]
. 我甚至尝试在 Javascript 中创建一个自定义函数来获取 URL 查询字符串,当我尝试显示它时,它只输出变量名称,而不是值本身。 有谁知道我做错了什么,并有想法解决这个问题?
下面是函数:
function google_admanager_dfp_add_js($js = NULL, $type = 'slot') {
static $ga_js = array();
// add the js to a type
if (isset($js) && isset($type)) {
$ga_js[$type][] = $js;
//add the init and service scripts the first time this is run
if (!isset($ga_js['service'])) {
if (variable_get('google_admanager_dfp_useasync', FALSE)) {
google_admanager_dfp_add_js("var googletag = googletag || {};googletag.cmd = googletag.cmd || [];", 'service');
google_admanager_dfp_add_js("(function() {var gads = document.createElement('script');gads.async = true;gads.type = 'text/javascript';var useSSL = 'https:' == document.location.protocol;gads.src = (useSSL ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js';var node = document.getElementsByTagName('script')[0];node.parentNode.insertBefore(gads, node);})();", 'service');
google_admanager_dfp_add_js("googletag.cmd.push(function() {", 'pre');
google_admanager_dfp_add_js("googletag.enableServices(); });", 'close');
} else {
google_admanager_dfp_add_js('(function() {var useSSL = ''https:'' == document.location.protocol;var src = (useSSL ? ''https:'' : ''http:'') +''//www.googletagservices.com/tag/js/gpt.js'';document.write(''<scr'' + ''ipt src="'' + src + ''"></scr'' + ''ipt>'');})();', 'service');
// set the close script to fetch the ads.
if (isset($_GET["test"])) {
// Line in question is below ->
google_admanager_dfp_add_js('googletag.pubads().setTargeting("test","");', 'close');
}
google_admanager_dfp_add_js('googletag.pubads().enableSyncRendering();googletag.enableServices();', 'close');
}
}
return;
}
您可以使用 location.search 获取该对,如下所示:
var query = window.location.search.substring(1);
var pair = query.split("=");
// ...
// Line in question is below ->
google_admanager_dfp_add_js('googletag.pubads().setTargeting('+pair[0]+','+pair[1]+');', 'close');
pair[0]
将被test
,pair[1]
将被5
.
http://www.mathias-bank.de/2007/04/21/jquery-plugin-geturlparam-version-2/
帮助我在一个简单的.js文件中完成了您要查找的内容。
感谢创建它的马蒂亚斯银行。