J'ai le code pour obtenir une partie de l'ancre:Comment modifier une partie de l'ancre dans l'URL
function _get_part(queryString, name) {
var match = '&' + name + '=';
var i = queryString.indexOf(match);
if(i < 0) {
match = name + '=';
if(queryString.slice(0, match.length) == match)
i = 0;
}
if(i > -1) {
i += match.length;
return queryString.slice(i, queryString.indexOf('&', i) >>> 0);
}
};
function get_location_hash() {
return window.location.hash.substr(2);
}
function get_part(name) {
return _get_part(get_location_hash(), name);
}
je besoin d'une fonction pour changer une partie de l'ancre, si cette partie existe, ou ajouter une partie si elle n'existe pas.
A cette époque, j'utilise le code suivant:
function set_part(queryString, key, value) {
var oldv = key + '=' + get_part(key);
var newv = key + '=' + value;
window.location.hash = '/' + queryString.replace(oldv, newv);
}
Mais si la partie de l'ancre n'existe pas, l'ancre ne change pas.
format URL: ... la page/#/var1 = var2 = & blablabla var2text & gghh = edere
Anchor - #/var1 = var2 = & blablabla var2text & gghh = edere
Désolé mon anglais.
Merci beaucoup!
mise à jour:
ce génial, merci beaucoup! un seul problème: je charger la page withoud des ancres: .../page/ nex utilisent ce code:
set_part(get_location_hash(), 'filter', 'data');
set_part(get_location_hash(), 'filter2', 'data2');
set_part(get_location_hash(), 'fdgfg', 'fdgfdg');
alert(get_part('fdgfg'));
et recevoir .../page/#/= & filtre = données & filtre2 = data2 & fdgfg = fdgfdg
comment supprimer '=' symbole?
c'est génial, merci beaucoup! un seul problème: je charge la page avec toutes les ancres: http: //.../page/ nex utilise ce code: set_part (get_location_hash(), 'filter', 'data'); set_part (get_location_hash(), 'filter2', 'data2'); set_part (get_location_hash(), 'fdgfg', 'fdgfdg'); alert (get_part ('fdgfg')); et de recevoir http: //.../page/#/=&filter=data&filter2=data2&fdgfg=fdgfdg comment supprimer d'abord '=' symbole? – user492996
oups. J'ai ajouté deux lignes au début de la boucle for dans '_parseQueryString()'. Cela va résoudre ce problème. – Lee
Merci beaucoup! – user492996