CGI and external JavaScript nightmare

Paul Boddie paul at boddie.org.uk
Tue Oct 16 12:16:44 CEST 2007


On 11 Okt, 08:23, IamIan <ian... at gmail.com> wrote:
> My website is built from a Python CGI and works great. I'm now
> including Google Ads, which requires two pieces of JavaScript; the
> first contains the display settings for the ads, and the second piece
> is a very lengthy garbled js file athttp://pagead2.googlesyndication.com/pagead/show_ads.js

Shudder! I don't think I'm even going to look at that.

> The first piece of JavaScript works fine and the ads display
> correctly, however the second file throws an "unterminated string
> literal" js error. Looking at it you can see a healthy amount of both
> single and double quotes, which is where my problem lies.

Are you including the JavaScript inline or using a script element in
your HTML?

> What is the best way to get the second js file into a nice printable/
> usable format? cgi.escape? xml.sax.saxutils.quoteattr? String
> substitution? I could use some guidance.

If you're just referencing the JavaScript using a script element, you
don't need to think about encoding, escaping or quoting it since the
browser will read it as text/x-javascript or something like that.
Otherwise, if you have a desperate need to include the JavaScript
inline (actually embedded in the HTML page), then it may help to
include it in a CDATA section. I recall various tricks that may be
necessary, especially when dealing with Internet Explorer brokenness,
but not having to serve Windows users any more (particularly those who
also like ActiveX and think it's part of the Web) means that I don't
care about or recall those tricks readily. Pretending that IE doesn't
support lots of standards usually sets you off on the right foot,
though.

Paul




More information about the Python-list mailing list