1

私は正常に動作するこのコードを持っています:

if (typeof jQuery === "undefined" || jQuery.fn.jquery !== '1.4.2') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src","http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
script_tag.onload = main; // Run main() once jQuery has loaded
script_tag.onreadystatechange = function () { // Same thing but for IE
if (this.readyState == 'complete' || this.readyState == 'loaded') main();
}
document.getElementsByTagName("head")[0].appendChild(script_tag);
} else {
    main();
}

しかし、ブックマークレターで圧縮しようとすると、「予期しない識別子」エラーが発生します

javascript:(function(){if(typeof%20jQuery===%22undefined%22%20||%20jQuery.fn.jquery%20!=='1.4.2'){var%20script_tag=document.createElement('script');script_tag.setAttribute(%22type%22,%22text/javascript%22);script_tag.setAttribute(%22src%22,%22http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js%22);script_tag.onload=main;script_tag.onreadystatechange=function(){if(this.readyState=='complete'%20||%20this.readyState=='loaded')main();}document.getElementsByTagName(%22head%22)[0].appendChild(script_tag);}else{main();}})();

提案?

ありがとう

4

1 に答える 1

4

document.getElementsByTagName("head")[0].appendChild(script_tag);これがブックマークレターのバグかどうかはわかりませんが、そのようにすべてを 1 行にまとめる場合は、前にセミコロンが必要です。

これを行うと、予期しない識別子エラーはなくなります。実際に以前に定義していない限り、「メインが定義されていません」という新しいエラーが発生しますmain

于 2012-07-15T19:38:00.790 に答える