homepage Welcome to WebmasterWorld Guest from 54.196.159.11
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
JS to doc.write literal code
making document.write actually write a string
mrblond




msg:4443459
 4:27 pm on Apr 20, 2012 (gmt 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:

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?

 

Fotiman




msg:4443468
 4:46 pm on Apr 20, 2012 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved