Welcome to WebmasterWorld Guest from 54.159.246.164

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)

5+ Year Member



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:

http://www.domain.com/item_info.cfm?prodnum=123-456-789-012


The correct image is here:

http://www.domain.com/resources/productimage.cfm?prodnum=123-456-789-012&size=medium


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");
-->
</script>


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)

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



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.