Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

JS to doc.write literal code

making document.write actually write a string

4:27 pm on Apr 20, 2012 (gmt 0)

New User

10+ Year Member

joined:Nov 1, 2005
posts: 27
votes: 0

I'm trying to implement Google Custom Search with images. I need to tell Google Custom Search were the related product image is.

I have a product at this url:


The correct image is here:


I'm using javascript to parse this URL to get the prodnum variable - here's the code:

<script type="text/javascript"><!--
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
return vars;

var prodnum = getUrlVars()["MyProdNum"];
document.write("<meta name='thumbnail' content='http://www.domain.com/resources/productimage.cfm?prodnum=" + MyProdNum + "&size=medium");

When I view source, this code is plainly displayed, not the URL string. I'd like to literally print the url string to the source of the page. Is this possible?
4:46 pm on Apr 20, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4988
votes: 12

First, don't put HTML comments inside script tags. They were needed for version 1 browsers only and haven't been needed for more than a decade.

document.write("<meta name='thumbnail' content='http://www.domain.com/resources/productimage.cfm?prodnum=" + MyProdNum + "&size=medium");

Did you mean:

document.write("<meta name='thumbnail' content='http://www.domain.com/resources/productimage.cfm?prodnum=" + prodNum + "&size=medium");

When I view source, this code is plainly displayed, not the URL string.

Yes, that's the expected behavior when you view the source, since this is the source. To literally print that URL string to the source of the page, you would need to use some server side technology, like PHP, ASP, etc.