0
votes

Comment obtenir la chaîne après '#' dans l'URL

J'essaie d'obtenir la chaîne disponible après # dans l'URL. fondamentalement, c'est un identifiant de l'élément qui est passé depuis une autre page.

Par exemple, l'URL ci-dessous a des investisseurs_brand après le caractère # , j'ai besoin d'obtenir cette chaîne avec jquery

var a = window.location.href;
console.log(a.slice('#'));

voici le code.

www.example.com/about_us#company_branding

Mais je n'ai pas pu faire les choses correctement.


6 Réponses :


2
votes

Utilisez split

var a = window.location.href;
console.log(a.split('#')[1]);

console.log('www.example.com/about_us#company_branding'.split('#')[1])


1 commentaires

index dur, ne devrait pas.



2
votes

utilisez window.location.hash

console.log(window.location.hash)


0 commentaires

0
votes

Vous pouvez utiliser la valeur de hachage:

https://www.w3schools.com/jsref/prop_loc_hash.asp

var x = location.hash;


0 commentaires

0
votes

Vous pouvez utiliser substring.

const hash = window.location.hash;
console.log(hash.substring(1));

Ceci renvoie la partie de la chaîne après l'index 1


0 commentaires

0
votes

Vous pouvez obtenir la valeur de hash :

window.location.hash.substring(1)

Si vous le souhaitez sans le # , utilisez:

window.location.hash


1 commentaires

En gros, ce que @apple a dit. J'étais en train de rédiger cette réponse en même temps.



0
votes

Essayez

var a = 'abc.com/blog/seo-google-123';
console.log(a.split('-').pop());

Résultat: 123


0 commentaires