2012-01-03 7 views
0

Jquery.JSがロードされているかどうかを確認してからロードする必要があります。以下のコードは、私の.aspxファイルでは正常に動作しますが、私は書く場合Jqueryのチェック方法は既にxsltのページにロードされています

ASPX動作するコードに、XSLTで実行中にエラーを与える私の.xsltのalso.Itsに

<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> 

を同じを実装する必要があります

<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> 

エラー:XSLTで同じASPXコードは、その投げエラー

XSLTコード "タグが閉じられていませんでした" 期待します。 1行目。

提案がありますか?以下のコードは、問題

<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) –

答えて

2

を作品 UPDATED

おかげで、あなたは私たちに示されていないXSLTコードです。

Iを試み、報告されたエラーを再現できませんでした - この変換は問題なく実行される:

<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

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

+0

@Vani:スクリプト登録に 'document.write'を使わないでください。必ずしも成功するとは限りません。より良い方法は 'document.createElement'で、これを文書の先頭に追加します。 –

関連する問題