LoginSignup
2
2

More than 5 years have passed since last update.

Greasemonkeyで読み込んだjQueryが本体のjQueryを上書きしてしまう

Posted at

Greasemonkeyを使う際は毎回jQueryを読み込んでいるが、user.jsで触った所以外のレイアウトが崩れる現象が発生。

原因を探ると適用側の読み込みjQueryとGreasemonkey側での読み込みjQueryのバージョン違いが原因でした。

軽く触れているのがこのあたりですが、周知の事実でしょうか。


結論

  • $.browserjQuery1.9から廃止された
  • 適用ページではjQuery1.5.2を使用していた
  • 適用ページでは$.browserを使ってIEのバージョン判定をしていた
  • GreasemonkeyではjQuery1.11.0を読み込んでいた
  • GreasemonkeyでのjQueryの読み込みをやめると、正常に戻り、Greasemonkey内でもjQueryは使えた
  • おそらく、GreasemonkeyがjQuery1.11.0を読み込むことで$が1.11.0仕様になった?

GreasemonkeyでjQueryが使いたいときは読み込み前に適用側のソースを確認しなければならない、ということでしょうか。
GreasemonkeyではjQueryをコピペで毎回
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js
と読み込んでいたが、汎用的にするには変数$ (jQuery)が未定義の場合に<script>を追加し、そこで読み込む…などとするのがいい…?

エラーが出たコード

$(document).ready(function() {
    if ( $.browser.msie ) {
    }
.
.
.
}

TypeError: $.browser is undefined

$.browseでエラーが出るため、それ以降の処理が行われなかった。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2