0

I have this java script that I insert into my sidebar. It is for a 350x200 ad banner.

<SCRIPT language="Javascript">
var cpmstar_rnd=Math.round(Math.random()*999999);
var cpmstar_pid=14214;
document.writeln("<SCR"+"IPT language='Javascript' src='http://server.cpmstar.com/view.aspx?poolid="+cpmstar_pid+"&script=1&rnd="+cpmstar_rnd+"'></SCR"+"IPT>");
</SCRIPT>

^ ^ This is the java script my ad network gave me, I cannot edit it

I want to load this ad banner as fast as possible without hindering the rest of my website.

Currently, what I do is I put this ad tag on a blank html page, then I call that html page using an IFRAME on my sidebar. I heard that IFrames load asynchronously.

Is there any better method that I could use?

3
  • why writeln? document.write() Commented May 25, 2011 at 6:30
  • I edited my post. I don't know why that is there, that's how I got my ad code. Commented May 25, 2011 at 6:35
  • At least you can make it look better - get rid of the language attribute, it was deprecated last millennium. The type attribute is required. The handling of "script" within the document.write string is also BS, all that is required is to quote the forward slash in </ like so: <\/. Commented May 25, 2011 at 7:01

3 Answers 3

1

If your concern is practical rather than academic, go ahead and use an iframe. An iframe is essentially an embedded window, and as such does indeed load asynchronously.

You could also use Ajax to load the script, but the path of least resistance is to use an iframe (again, assuming you're more concerned with just getting it to work rather than ensuring your site validates -- if your doctype is XHTML Strict and you're serving your content as XML, you shouldn't use iframes, but I have a hunch you're not doing that).

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

3 Comments

In regard to the defer attribute - it may make a difference, but probably not. It's just a hint to the browser, most will ignore it. If the OP is using document.write then you can guarantee the content isn't being served as XML (and it would fail validation anyway as posted).
Some modern browsers support async="async" which is slightly faster than defer (it will allow both page rendering and script execution to continure, while defer still executes scripts in order).
@RobG, I've seen defer make a noticeable difference in page rendering time (I work at a company that serves widgets with ads from various ad networks). What I forgot, however, is that deferring a script with doc.write doesn't make sense since it will just overwrite the page content, so I'm modifying my answer. As for failing validation, I addressed that point.
0

start by updating your code

<script language="Javascript" type='text/javascript'>
var cpmstar_rnd=Math.round(Math.random()*999999);
var cpmstar_pid=14214;
document.writeln("<scr"+"ipt language='Javascript' src='http://server.cpmstar.com/view.aspx?poolid="+cpmstar_pid+"&script=1&rnd="+cpmstar_rnd+"'></scr"+"ipt>");
</script>

Comments

0

Iframes load asynchronously, but even if they didn't, embedding an untrusted external script in your page is a huge security hole, so it is good practice to use an iframe (if possible, one with a different domain) anyway.

Apart from that, an iframe might be slightly slower than asynchronous script loading via ajax due to the extra page request, but it is a lot simpler to maintain.

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.