2014-08-29 40 views
0

我的网站目前正在使用Volusion电子商务平台。正如你可能会或可能不知道的那样,几乎整个系统的后端都被锁定了。为了让我在我的所有产品页面中添加额外的html内容,我通过Volusion支持向我的模板文件添加了以下一段Java脚本。document.write针对电子商务平台预先设计的代码

这个例子说明了如何可以添加一些HTML(这一个简单的“极品 帮助?联系我们!”消息)出现在每一个产品 页面的底部。它可以放在后面使用的“content_area”标识结束标记匹配的div 标签:

<script type="text/javascript"> 
//<![CDATA[ 
if (location.pathname == "/ProductDetails.asp" || location.pathname.indexOf("-p/") != -1 || location.pathname.indexOf("_p/") != -1) 
    document.writeln("\n<table align='center' style='border: solid 2px black;'><tr><td align='center' style='background-color:#ccc; font-weight: normal; padding:10px;'><span style='font-size: larger; font-weight: bold;'>Need help on this or any other product?</span><br />Call 1-800-YourNumberHere | <a href='mailto:[email protected]'>E-mail Us</a> | <a href='/help.asp'>Help On Our Site</a> | <a href='/returns.asp'>Return Policy</a></td></tr></table>\n\n"); 
//]]> 
</script> 

然而,在简单地增加这个股票代码,以我的模板文件,它似乎无法正常工作。我试图在满足url要求的页面上查看它。作为Java脚本的新手,我能够搜集到的唯一代码就是,在描述代码的时候,他们说它的目标是“使用content_area的ID标记div标签”,我没有看到该定位发生在代码中的哪个位置。

任何帮助,将不胜感激。我的产品页面URL全部以“-p /”结尾,所以我不需要代码中的其他条件。

谢谢!

+0

您已将jQuery标记添加到问题中。你愿意使用它吗? – melancia 2014-08-29 13:53:03

+0

绝对!这只是由volusion给我的股票代码 – 2014-08-29 14:03:27

+0

像这样? http://jsfiddle.net/MelanciaUK/7nwg8mL3/ – melancia 2014-08-29 14:09:03

回答

1

正如你说这将是确定使用jQuery:

$(function() { 
    if (location.pathname == "/ProductDetails.asp" || location.pathname.indexOf("-p/") != -1) { 
     var content = "<table align='center' style='border: solid 2px black;'><tr><td align='center' style='background-color:#ccc; font-weight: normal; padding:10px;'><span style='font-size: larger; font-weight: bold;'>Need help on this or any other product?</span><br />Call 1-800-YourNumberHere | <a href='mailto:[email protected]'>E-mail Us</a> | <a href='/help.asp'>Help On Our Site</a> | <a href='/returns.asp'>Return Policy</a></td></tr></table>"; 

     var el = $('#content_area'); 

     // If there's an element with id = content_area in the page, 
     // let's insert the content after it. 
     // Otherwise, let's insert it to the body. 
     if (el != null) { 
      $(el).after(content); 
     } 
     else { 
      $('body').append(content); 
     } 
    } 
}); 

注意:如果页面没有jQuery库加载,你需要做的,上面的代码之前:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
+0

完美。谢谢! – 2014-08-29 14:19:49