开发者

Javascript conflict over string.replace method

I am using a javascript code to display an ad in my page . The javascript code for the corresponding ad display is as shown below :

var ad = {
    encode: f开发者_开发知识库unction(str) {
        return escape(this._utf8_encode(str));
    },

    _utf8_encode: function(str) {
        str = str.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < str.length; n++) {

            var c = str.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }
        return utftext;
    }
}

var partnerId = "b7241ebe72fa74ce";
var siteId = "11738";

var targetparams = new Array();
targetparams['ad_platform'] = "ros";
var targetparams_str = "";
for (var key in targetparams) {
    if (targetparams_str != "") {
        targetparams_str += "||";
    }
    targetparams_str += ad.encode(key) + "=>" + ad.encode(targetparams[key]);
}
targetparams_str = ad.encode(targetparams_str);

var m3_u = 'http://ads.admarvel.com/fam/javascriptGetAd.php';
var m3_r = Math.floor(Math.random() * 99999999999);
document.write("<scr" + "ipt type='text/javascript' src='" + m3_u);
document.write("?partner_id=" + partnerId);
document.write('&amp;site_id=' + siteId);
document.write('&amp;target_params=' + targetparams_str);
document.write('&amp;version=1.5');
document.write('&amp;language=javascript');
document.write('&amp;format=wap');
document.write('&amp;cb=' + m3_r);
document.write("'><\/scr" + "ipt>");

The above code is inside a element in the HTML body. Now in my page I want to include an external js library protoaculous.1.8.2.min.js and when I place this js file in the head , somehow , my ad disapperars , but as soon as I remove the external library , the ad is displayed .

The outline of my page is as below :

<head>
<script type="text/javascript" src="http://www.ectnews.com/shared/ajax/protoaculous.1.8.2.min.js"></script>
<body>
    <div>
        //Javascript code for the ad display
    </div>
</body>

Following are the links to the scenario I have created in my test server :

Non-Working html :

http://m.smartdevicemedia.com/test_external_js.htm

Working html :

http://m.smartdevicemedia.com/test_external_js_good.htm


I suspect that the problem is here:

var targetparams = new Array();
targetparams['ad_platform'] = "ros";
var targetparams_str = "";
for (var key in targetparams) {
    if (targetparams_str != "") {
        targetparams_str += "||";
    }
    targetparams_str += ad.encode(key) + "=>" + ad.encode(targetparams[key]);
}

The Prototype library adds a bunch of methods to the Array prototype, and your "for ... in" loop will see those. There's no reason for "targetparams" to be an Array instance, however. It should just be an object:

var targetparams = {};

In fact, from that code above there's no reason for "targetparams" to even exist. You could simply initialize the string to the single key that you've actually got hard-coded there in the lines above the loop:

var targetparams_str = ad.encode("ad_platform") + "=>" + ad.encode("ros");

It also seems very odd that you'd re-encode the whole string after building up the string from encoded pieces.


Check your dev console:

Uncaught TypeError: Object function (C,B){var A=0;try{this._each(function(E){C.call(B,E,A++)})}catch(D){if(D!=$break){throw D}}return this} has no method 'replace'

test_external_js.htm:45

This Line:

string = string.replace(/\r\n/g,"\n");

Now as to WHY it does this...


You can not put that code in the head with document.writes out html markup tags.You would put the code where you want the ad to appear on the page.

<head>
</head>
<body>
    <div>
        <script type="text/javascript" src="yourAdCode.js"></script>
    </div>
</body>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜