0
I have a URL, https://meusite.com/q6uzeyln32td/naoquero.html, and I need to 'cut' the code between the bars (q6uzeyln32td) for a variable. I have tried the split() method with the following code:
var mainURL = window.location.href;
mainURL = mainURL.split('.com/');
However, it always returns an array like this:
Array [ "https://site", "q6uzeyln32td/naoquero.html" ]
The value I want is with others I don’t want.
There are numerous ways to do this. I wouldn’t say that the
split
be the best solution, but the best way would also depend on the restrictions you may have.– tvdias
A solution still with the
split
would use only the/
and take position 3 of the generated array.– tvdias
You have can use
window.location.pathname
, in place ofwindow.location.href
and will already have the URL without the protocol and domain.– tvdias
Great, I modified and worked normally, without having to make the request twice... Thanks @tvdias
– Diego Queiroz
When you can (if I’m not mistaken for the least time), add a reply and mark it as accepted.
– tvdias
AP used the question itself to answer. The question was reverted to the first version and the answer was put as wiki.
– Augusto Vasques
window.location.pathname.split('/')[1]
– hkotsubo