css('background-image') パスを置き換えたい。
問題:
同じ変数に対してoldBgImg = this.element.css('background-image')
FireFox が復活 -
"url("http://mySite/images/file1.png")"
しかしChromeは引用符なしでそれを返します:
"url(http://mySite/images/file1.png)"
これが私が使用するソリューションです。もっと簡単にするのを手伝ってもらえますか?
var oldBgImg = this.element.css('background-image');
// => FF: "url("http://mySite/images/file1.png")"
// Chrome: "url(http://mySite/images/file1.png)"
// According to http://www.w3.org/TR/CSS2/syndata.html#value-def-uri :
// quotes are optional, so Chrome does not use them, but FF does . . .
var n1 = oldBgImg.lastIndexOf("("); n1 += 1; // now points to the char after the "("
var n2 = oldBgImg.lastIndexOf(")"); n2 -= 1; // now points to the char before the ")"
var c1 = oldBgImg.substring(n1, n1 + 1); // test the first Char after the "("
var c2 = oldBgImg.substring(n2, n2 + 1); // test the first Char after the "("
if ( (c1 == "\"") || (c1 == "\'") ) { n1 += 1; }
if ( (c2 == "\"") || (c2 == "\'") ) { n2 -= 1; }
var oldBgImgPath = oldBgImg.substring(n1, n2 + 1); // [ (" ] .. [ ") ]
var n = oldBgImgPath.lastIndexOf("/");
var newBgImgPath = oldBgImgPath.substring(0, n + 1) + "file2.gif";
// if needed, should also add :
// var path = encodeURI(newBgImgPath);
this.element.css('background-image', 'url(' + newBgImgPath + ')');
ノート:
http://www.w3.org/TR/CSS2/syndata.html#value-def-uriによると、 一重引用符または二重引用符を使用するか、引用符なしで使用できます
相対パス (「http」なしまたは「ファイル」あり) の一般的な解決策を探しています。URL 内の fileName を置き換えたいだけです。