Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

javascript - document.write nested in document.write

I have an ad tag that a third party is trying to stuff inside of a 'document.write' function and it's not working because the ad tag itself also contains document.write's. Is there a way to shove this ad tag inside of a single instance of document.write? If so, please help me figure this out and if not, is there an alternative?

<script type='text/javascript'>
var m3_u = 'http://this.that.com/adtag.js';
var m3_r = Math.floor(Math.random() * 99999999999);
var category='999';

if (!document.MAX_used) 
    document.MAX_used = ',';

document.write("<scr" + "ipt type='text/javascript' src='" + m3_u);
document.write("?c=" + category +"&amp;b=Sampletag&amp;p=ptnr&amp;key=4984cc8f3064e22a4e29fb2b3b2e9cb5");
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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

document.write is often concidered a harmful method, as it directly inserts content into the document file itself. You should edit the innerHTML of the tag where you want to insert the code, although I've heard that directly using innerHTML isn't the correct way either. The method is called insertNode, If I'm remembering correctly, but I'm not sure, because I'm usually abstracting this type of problem away using frameworks such as jQuery, where it is as simple as

$("#myelement").html("<script>...</script>")

I hope that some of my fellow SO members can make this post more precise, I'll look some stuff up myself, but concider this to be my quick answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...