2012-10-05 3 views
0

Google 애널리틱스 태그에서 특수 문자를 제거하려고합니다. 다음 스크립트가 있는데 작동하지 않는 이유를 알 수 없습니다. JSP와 자바 스크립트가 혼합되어 있습니다. 직장 학습 중 ...함수가 호출되지 않았습니다. - JavaScript 및 JQuery를 시도했습니다.

   <script type="text/javascript"> 
       function removeSplChars(inStr) { 
       inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, ""); 
       return inStr; 
       } 
       var _gaq = _gaq || []; 
       _gaq.push(['_setAccount', '<c:out value="${profileId}"/>']); 
       <c:choose> 
       <c:when test="${(lastCmdName eq 'CategoryDisplay') or (lastCmdName eq 'ProductDisplay')}" > 
       _gaq.push(['_setCustomVar', 
       2, // This custom var is set to slot #2. 
       '<c:choose><c:when test="${WCParam.source eq 'search'}">Search</c:when><c:otherwise><c:out value="${topCat}" /></c:otherwise></c:choose>', // The top-level name for your online content categories. 
       '<c:choose><c:when test="${WCParam.source eq 'search'}">Search <c:out value="${WCParam.searchTerm}" /></c:when><c:otherwise><c:out value="${topCat}" />|<c:out value="${subCatA}" />|<c:out value="${subCatB}" />|<c:out value="${subCatC}" /></c:otherwise></c:choose>', // Records value of breadcrumb name 
       3 // Sets the scope to page-level. 
       ]); 
       </c:when> 
       <c:otherwise> 
       </c:otherwise> 
       </c:choose> 
       removeSplChars(<c:out value="${topCat}" />, <c:out value="${subCatA}" />, <c:out value="${subCatB}" />, <c:out value="${subCatC}" />); 
       _gaq.push(['_trackPageview']); 
       (function() { 
       var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
       ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
       var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
       })(); 
       </script> 

일부() 또는 다른 것을 잃어 버렸습니까? 나는 예상치 못한 토큰을 얻는다. 콘솔에 오류 메시지가 표시됩니다. $ (document) .ready (function() {}) 내에 removeSplChars에 대한 호출을 시도했습니다.

을 사용할 수 없습니다.

나는 다음과 같은 게시물을 확인했지만 조언은 나에게 도움이되지 않습니다 JavaScript Remove special characters string not working

감사합니다 : Function is not Called javascript function not getting called

내가 여기에 또 다른 게시물을합니다. removeSplChars에 전달

   <script type="text/javascript"> 
       function removeSplChars(inStr) { 
       inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, ""); 
       return inStr; 
       }  
       var _gaq = _gaq || []; 
       _gaq.push(['_setAccount', 'UA-33021136-1']); 


       _gaq.push(['_setCustomVar', 
       2, // This custom var is set to slot #2. 
       'Dine &amp; Entertain', // The top-level name for your online content categories. 
       'Dine &amp; Entertain|Bowls &amp; Platters||', // Records value of breadcrumb name 
       3 // Sets the scope to page-level. 
       ]); 



       $(document).ready(function() { 
       removeSplChars(Dine &amp; Entertain, Bowls &amp; Platters, ,); 
       }); 
       _gaq.push(['_trackPageview']); 

       (function() { 
       var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
       ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
       var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
       })(); 

       </script> 
+3

서버 측 마크 업이 아닌 브라우저 [보기 소스]에서 생성 된 코드를 확인하는 것이 항상 좋습니다. – epascarello

+0

@epascarello, 고맙습니다. 코드를 표시하기 위해 내 게시물을 업데이트했습니다. 나는 당신이 제공 할 수있는 어떤 도움에 대해서도 매우 감사 할 것입니다. – surfbird0713

답변

0

귀하의 문자열을 따옴표로 묶여 있습니다, 그래서 당신이 오류를 얻을 : 소스보기를 통해 볼 수 있듯이 코드를 추가

. 가능한 경우 JSON 인코딩 후 변수를 전달하는 것이 더 신뢰할 수 있습니다. 또는 "을 입력하고 최상의 결과를 얻으실 수 있습니다 (입력시 "&quot;으로 인코딩 한 경우 정상적으로 작동 함).

또한 빈 인수로 인해 발생하는 문제를 처리합니다. 대신 빈 문자열이 인수로 전달됩니다.

+0

감사합니다. 따옴표를 추가하고 더 이상 오류가 발생하지 않지만 여전히 문자열에 특수 문자가 표시됩니다. 뭐라 구요? 이것은 나를 바나나로 몰고 간다. – surfbird0713

+0

따옴표를 추가했는데 왜 작동하지 않는지 알 수 있습니까? – surfbird0713

관련 문제