2016-06-28 9 views
1

私は私のクリック()機能更新URLのonClick

var str = window.location; // http://localhost:8888/000D6766F2F6/10001/edit/private/basic 
var new_str = str.replace("basic", "a"); 
alert(new_str); 
location.href = new_str; 

私は

はなかった

enter image description here


を取得保管側に私の現在のURLを更新しようとしています私はここにはないと思う何かをする?

答えて

3

あなたは近くにいました。あなたの置き換えステートメントは、urlを途中で変更しています。あなたがwindow.locationのからプロパティ 'のhref' を取得する必要があり

var new_str = window.location.href.replace("basic", "a"); 
alert(new_str); 
window.location.href = new_str; 
1

var str = window.location.href; // << Get href 
var new_str = str.replace("basic", "a"); 
alert(new_str); 
location.href = new_str; 
2

window.locationはそれだけで文字列表現を持って、文字列ではありません。したがって、あなたが呼んでいる.replaceメソッドは、String.replaceメソッドではありません。実際にはthe location replacement methodです。新しい履歴エントリを追加せずに新しいページに移動し、何も返さない/ undefinedです。

あなたがString(または同等の.hrefプロパティにアクセス)に変換する場合は、あなたのコードは動作します:

var str = String(window.location); // http://localhost:8888/000D6766F2F6/10001/edit/private/basic 
var new_str = str.replace("basic", "a"); 
alert(new_str); 
location.href = new_str;