簡體   English   中英

將URL查詢字符串傳遞給Javascript函數

[英]Passing URL query string to Javascript function

我正在嘗試將查詢字符串從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]將為testpair[1]將為5

http://www.mathias-bank.de/2007/04/21/jquery-plugin-geturlparam-version-2/

幫助我在一個簡單的.js文件中執行所需的操作。

歸功於創建它的Mathias Bank。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM