0

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: function(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

1
  • Could you indent less? Can't see half of your code. Commented Sep 20, 2011 at 12:19

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You may have something here - when I look at the values of targetparams_str in both pages, I get ad_platform=>ros|| in the 'bad' page (see my answer for why) and ad_platform%3D%3Eros in the 'good' page. Notice that the good page doesn't have the '||' at the end.
0

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...

5 Comments

ya...I have seen that...Even , I am surprised, somehow the external js script modifies the methods of the String class? Even if you comment this line it will throw a error in the immidiate next line containing the string object.
@debaShish: Well, did you try changing the name to something neutral, like 'theValue'?
ya...also tried that ...but it gave the error like : "Object doesn't support this method or property"
@debaShish: Of course, it all makes sense now! Pointy has it - Prototype adds all kinds of stuff to Objects, and 'for...in' loops break if you don't add something like if (!targetparams.hasOwnProperty(key)) continue. The code works for the first iteration through the loop, but the second time in, it calls '_utf8_encode' with one of the Prototype addons - which is an Object, not a String.
thanks buddy , I also saw the error . I shouldn't be calling the '_utf8_encode' function with targetparams[key] as it is an Object here. Thanks for pointing this out.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.