私が実行したときに、なぜ私は... ...「string.splitは関数ではありません」というエラーの原因は何ですか?
Uncaught TypeError: string.split is not a function
を取得しています...
(function() {
var string = document.location;
var split = string.split('/');
})();
私が実行したときに、なぜ私は... ...「string.splitは関数ではありません」というエラーの原因は何ですか?
Uncaught TypeError: string.split is not a function
を取得しています...
(function() {
var string = document.location;
var split = string.split('/');
})();
変更この...
var string = document.location;
これに...
var string = document.location + '';
document.location
はLocation objectです。デフォルトの.toString()
は文字列形式で位置を返します。連結するとその位置がトリガーされます。
また、document.URL
を使用して文字列を取得することもできます。あなたは現在のURL
ハッキー連結の代わりに' toString() 'を呼び出す方がクリーンではないでしょうか? – kapa
@bažmegakapa:それは好みの問題です。 '+ ''は文字列強制の非常に一般的なトリックですが、' toString() 'メソッドを好む人もいます。私は数値変換に単項 '+'を使うよりも、それ以上の面倒は考えていません。 –
それはちょうど醜いです。 'parseInt()'と 'parseFloat()'があります。 'Number()'もあります。 '+'は当然短くなりますが、コードをハックするのに慣れていない人や経験の少ない人にとっては読みにくくなります。 – kapa
多分
string = document.location.href;
arrayOfStrings = string.toString().split('/');
。
おそらくdocument.location.href
またはdocument.location.pathname
を代わりに使用したいと考えています。この
// you'll see that it prints Object
console.log(typeof document.location);
document.location
が文字列でないと仮定したい
笑。同時に4つの答え(少なくとも)。私はSOに関する最新の質問を見るべきではありません:) –
実行あなたはdocument.location`がオブジェクトである `document.location.toString()
またはdocument.location.href
ありがとう。私はvarを文字列からオブジェクトに変換したことを認識していませんでした。あなたのソリューションは私のコードをチェックバックするアイデアを与えました。 – sg552
をしたいです。 try: 'var string = document.location.href' – Teemu