停止变量干扰/变量范围

时间:2018-08-02 06:21:39

标签: javascript variables global-variables

我有一些代码想在页面上重复,(下面是完整的代码)它多次使用相同的变量名,一次加载时,它可以正常工作,多次加载时,由于变量而中断互相干扰。

是否有一个简单的解决方法,而无需手动检查并为每次重复的代码更改每个变量的名称?

<body style="margin:0;padding:0">

<script>
    //vertical script

    //possible values: layout1,layout2,layout3,layout4 (1mweb, 2tablet, 3,4 desktop)
    var area = "%%AREA%%"
    var subArea = "%%SUBAREA%%"
    var adUnitMacro = area + subArea
    var verticalOutput = ""

    keywordArray = ["news", "homepage", "technology", "entertainment", "sport", "business", "lifestyle", "travel", "motoring", "property"]

    for (i = 0; i < keywordArray.length; i++) {
        if (adUnitMacro.includes(keywordArray[i])) {
            verticalOutput += keywordArray[i] + ",";
        }
    }
    if (verticalOutput.length > 1) {
        verticalOutput = verticalOutput.slice(0, -1);
    }

    if (verticalOutput.length <= 0) {
        verticalOutput = "news";
    }

    if (area.length <= 0) {
        verticalOutput = "homepage,news";
    }

</script>

<script>
    // Define the sizes here:

    var adUnitSizes = ["%%X_LAYOUT_1%%"];
    var rSizeOutput = [];

    if (adUnitSizes === [[300,250],[300,600]]){
        rSizeOutput = [15,10];
    }else if  (adUnitSizes === [728,90]){
        rSizeOutput = [2];
    }else if (adUnitSizes === [300,250]){
        rSizeOutput = [15];
    }


    // Add your post-bid tag IDs here:
    var adUnitBids = [{
        bidder: "appnexus",
        sizes: adUnitSizes,
        params: {
            placementId: "12895671",
            vertical: verticalOutput,
            kw_vertical: verticalOutput,
            kw_postbid: "true",
            postbid: "true"
        }
    }, {
        bidder: "rubicon",
        params: {
            accountId: "14232",
            siteId: "177830",
            zoneId: "864590",
            sizes: rSizeOutput,
            keywords: [verticalOutput],
            inventory: { "vertical": [verticalOutput] }
        }
    }]

    // Define the passback HTML tag here. Note that this tag is usually in either Script tag form or iFrame form. For example:

    /* iFrame:
        var passbackTagHtml = "<iframe src="http://ad.doubleclick.net/...;sz=300x250;ord=[timestamp]?" width="300" height="250" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no"></iframe>"
    */

    /* Script:
        var passbackTagHtml = "<scr" + "ipt type="text/javascript" src="https://www.googletagservices.com/tag/js/gpt.js">googletag.pubads().definePassback("/19968/header-bid-passback-tag", [300, 250]).display();</scr" + "ipt>"
    */

    var passbackTagHtml = "<script type=\"text\/javascript\" src=\"https:\/\/ads.odt.co.nz\/apl\/jserver\/SASPB\/FCID=%%FCID%%\"><\/scr" + "ipt>";



    // ======= DO NOT EDIT BELOW THIS LINE =========== //

    var pbjs = pbjs || {};
    pbjs.que = pbjs.que || [];

    (function () {
        var pbjsEl = document.createElement("script");
        pbjsEl.type = "text/javascript";
        pbjsEl.async = true;
        var isHttps = "https:" === document.location.protocol;
        pbjsEl.src = "//acdn.adnxs.com/prebid/not-for-prod/prebid.js";
        var pbjsTargetEl = document.getElementsByTagName("head")[0];
        pbjsTargetEl.insertBefore(pbjsEl, pbjsTargetEl.firstChild);
    })();

    var doNotChange = "doNotChange";

    pbjs.que.push(function () {

        var adUnits = [{
            code: doNotChange,
            sizes: adUnitSizes,
            bids: adUnitBids
        }];

        pbjs.addAdUnits(adUnits);

        pbjs.requestBids({
            timeout: 1500,
            bidsBackHandler: function () {
                var iframe = document.getElementById("postbid_if");

                var iframeDoc = iframe.contentWindow.document;

                var params = pbjs.getAdserverTargetingForAdUnitCode(doNotChange);

                // If any bidders return any creatives
                if (params && params["hb_adid"]) {
                    pbjs.renderAd(iframeDoc, params["hb_adid"]);
                    var frame = window.frameElement;
                    frame.width = document.getElementById("postbid_if").width;
                    frame.height = document.getElementById("postbid_if").height;
                } else {
                    // If no bidder return any creatives,
                    // Passback 3rd party tag in Javascript

                    iframe.width = adUnitSizes[0][0];
                    if (layoutPos === "layout1" || layoutPos === "layout2") {
                        iframe.height = adUnitSizes[0][1];
                    } else if (layoutPos === "layout3" || layoutPos === "layout4") {
                        iframe.height = adUnitSizes[1][1];
                    }
                    iframeDoc.write(passbackTagHtml);
                    iframeDoc.close();


                    iframeDoc.write(passbackTagHtml);
                    iframeDoc.close();
                }
            }
        });
    });

</script>

<iframe id="postbid_if" FRAMEBORDER="0" SCROLLING="no" MARGINHEIGHT="0" MARGINWIDTH="0" TOPMARGIN="0" LEFTMARGIN="0" ALLOWTRANSPARENCY="true"
    WIDTH="0" HEIGHT="0"></iframe>

0 个答案:

没有答案