0

I'm trying to convert our ads code from HTML to Javascript.

Here is the html

    <div id="mobile">
    <script><![CDATA[
    var m3_u = (location.protocol=='https:'?'https://localhost':'http://localhost');
    var m3_r = Math.floor(Math.random()*99999999999);
    if (!document.MAX_used) document.MAX_used = ',';
    document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
    document.write ("?zoneid=640&amp;target=_blank");
    document.write ('&amp;cb=' + m3_r);
    if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
    document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
    document.write ("&amp;loc=" + escape(window.location));
    if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
    if (document.context) document.write ("&context=" + escape(document.context));
    if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
    document.write ("'><\/scr"+"ipt>");
    ]]></script>
    </div>

Here is the code modified for javascript files.

FYI:

newelement creates us a new element with the ID mobile and attached to the comments element

    var elm=newelement("div",el("comments"),0,"mobile",""); // <div id="mobile"></div>
    var m3_u = (location.protocol=='https:'?'https://localhost':'http://localhost');
    var m3_r = Math.floor(Math.random()*99999999999);
    if (!document.MAX_used) document.MAX_used = ',';
    var str= "<scr"+"ipt type='text/javascript' src='"+m3_u;
    str+="?zoneid=640&amp;target=_blank";
    str+='&amp;cb=' + m3_r;
    if (document.MAX_used != ',') str+="&amp;exclude=" + document.MAX_used;
    str+= (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
    str+= ("&amp;loc=" + escape(window.location));
    if (document.referrer) str+="&amp;referer=" + escape(document.referrer);
    if (document.context) str+="&context=" + escape(document.context);
    if (document.mmm_fo) str+="&amp;mmm_fo=1";
    str+="'><\/scr"+"ipt>";
    elm.innerHTML=str;

My end goal is to have the javascript code inside the mobile div to load into the page from the javascript file instead of the html.

I tried appending str to the innerHTML of the common-mobile-ad div i created but that doesn't seem to work.What can I do to resolve this? Is my string concatenation correct?

somejkuser
  • 8,856
  • 20
  • 64
  • 130
  • Just `eval('javascriptstring')` the string version of your javascript here. Inb4 I get crucified for mentioning eval :D – Dellirium Aug 16 '16 at 15:52
  • @RolandStarke fixed look at question again – somejkuser Aug 16 '16 at 15:53
  • @Dellirium Can you make a solvable answer with your solution? – somejkuser Aug 16 '16 at 15:55
  • Actually, you'd be better off putting it in an autorunning function. – Adam Aug 16 '16 at 15:55
  • I will try but your HTML is horrendous. – Dellirium Aug 16 '16 at 15:58
  • @Dellirium I tried an eval and it created the blocks but the ad code didnt go through. i think that there might be an issue with my document.write to str+= logic. I aslo got: SyntaxError: expected expression, got '<' on conslle – somejkuser Aug 16 '16 at 16:00
  • 1
    @jkushner I cannot read the HTML you have, it is scrambled with a lots of other things, here is an example of how it works. https://jsfiddle.net/f457jk7b/ – Dellirium Aug 16 '16 at 16:17
  • @Dellirium I'll fix the question check back around 3ish. Also, I do need the script tags in my code as the src is an external api – somejkuser Aug 16 '16 at 16:40
  • If the source is externam API then you need to use the "string code" in it, to add the script tag. – Dellirium Aug 17 '16 at 09:30

0 Answers0