2014-08-29 84 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