2012-01-03 77 views
0

我需要檢查Jquery.JS是否已加載,如果沒有加載它。下面的代碼工作在我的.aspx文件很好,但我需要實現同我.xslt also.Its給錯誤在XSLT運行時,如何檢查jquery已經加載到頁面中xslt

ASPX工作代碼

<script type="text/javascript"> 
    window.jQuery || document.write('<script src="JQuery/jquery-1.3.2.min.js">\x3C/script>') 
    </script>  
<script type="text/javascript"> 
    $(document).ready(function() { 
     alert("hi"); 
    }); 
    </script> 

如果我寫在XSLT同一ASPX代碼,它的投擲 「標籤沒有被關閉」 錯誤

XSLT代碼:

<script type="text/javascript"> 
**line1**  (window.jQuery === undefined) and document.write(unescape('%3Cscript src="/js/jquery.js"')) 
</script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
    alert("hi"); 
    }); 
    </script> 

錯誤: 預期;在第一行。

有什麼建議嗎?

感謝, 修訂 下面的代碼工作

<script type="text/javascript"> 
     if(typeof jQuery=="undefined") 
     { 
     document.write(unescape("%3Cscript src='/js/jquery.js' type='text/javascript'%3E%3C/script%3E")); 
     }  
    </script>  
+0

不使用用於腳本註冊的'document.write'。它並不總是成功。更好的方法是'document.createElement'並將其追加到文檔頭部。 [看到這個答案](http://stackoverflow.com/a/8722371/880434) – 2012-01-06 04:09:44

回答

2

的問題是,你有沒有向我們展示的XSLT代碼。

我試過,無法生育的報道錯誤 - 這一轉型是沒有任何問題的執行:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/"> 
    <script type="text/javascript"> **line1**  (window.jQuery === undefined) and document.write(unescape('%3Cscript src="/js/jquery.js"')) </script> 
    <script type="text/javascript">  $(document).ready(function() {  alert("hi");  }); </script> 
</xsl:template> 
</xsl:stylesheet> 

當任何XML文檔(未使用)應用,結果如預期

<script type="text/javascript"> **line1**  (window.jQuery === undefined) and document.write(unescape('%3Cscript src="/js/jquery.js"')) </script> 
<script type="text/javascript">  $(document).ready(function() {  alert("hi");  }); </script> 
+0

我解決了這個問題,但感謝您的解決方案。 – Vani 2012-01-04 15:35:49

1

試試下面的代碼,

<script type="text/javascript"> 
    if (typeof jQuery == 'undefined') { 
     (function() {var jq = document.createElement('script');jq.type = 'text/javascript';jq.async = true;jq.src = '/js/jquery.js';var s = document.body.getElementsByTagName('script')[0];s.parentNode.insertBefore(jq, s);})(); 
    } 
</script> 
+0

我解決了這個問題,但感謝您的解決方案。 – Vani 2012-01-04 15:35:37

+0

@Vani:不要使用'document.write'來進行腳本註冊。它並不總是成功。更好的方法是'document.createElement'並將其追加到文檔頭部。 – 2012-01-05 03:41:33

相關問題