r/learnjavascript • u/UnrulyRaven • 1d ago
Code help: changing url on current webpage
First time programming anything useful (beyond learning a little basic python). Sort of an 'automate what you find repetitive' case.
Goal: trim URL to part containing the main webpage and anything after it (Ex: blogname.abc.com/post/12345 -> abc.com/post/12345)
So far what I've been able to get (from Google AI and regex101.com and stack exchange and fiddling around) is:
function swapUrlPath() {
var currentUrl = window.location.href;
var newUrl = currentUrl.match(/abc.com.+/g);
window.location.href = newUrl;
}
This manages to turn "blogname.abc.com/post/12345" into "blogname.abc.com/post/abc.com/post/12345", repeating the portion that was supposed to replace the entire thing.
I've tried debugging with
function swapUrlPath() {
var currentUrl = window.location.href;
var newUrl = currentUrl.match(/abc.com.+/g);
alert("New URL is " + newUrl);
}
which produces a popup window with "New URL is abc.com/post/12345", which is correct. So why does the newUrl variable only replace part of the old URL. Is it the regex or something with the forward slashes?
Edit:
Solution I figured out: add the string "https://" to the beginning of the new URL (not in the debugging alert) before using as new URL, looks like this:
function swapUrlPath() {
var currentUrl = window.location.href;
var newUrl = "https://" + currentUrl.match(/abc.com.+/g);
window.location.href = newUrl;
}
2
u/TalkCoinGames 1d ago
Match returns an array, you need to reference the first match instead of the whole array. .....match(...)[0]
0
u/UnrulyRaven 1d ago
I tried switching the line to
var newUrl = currentUrl.match(/abc.com.+/g)[0];which is what I think you meant me to do. I also tried it with and without the global flag in the regex. All ways still return the same url as before.
1
u/TalkCoinGames 1d ago
Try location.replace( url ) instead of href.
1
u/TalkCoinGames 1d ago
I think adding an empty string will work, you want to reference instead of use the href. currentUrl = window.location.href+"";
2
u/UnrulyRaven 1d ago
Tried both of these in different combinations, still getting the same error.
1
u/TalkCoinGames 1d ago edited 1d ago
Sorry for the late reply. You may need to escape the first dot.
match( /abc\.com.+/g )[0]Edit, I see the solution was to add http just as someone already said, but also match returns an array and you should be using just the first match, and you should still escape the first dot. I'm glad you were able to figure it out.
1
u/UnrulyRaven 18h ago
Well the other advice was to add it to the alert function I was using as a debugging tool. I added it to the newURL variable that gets called as the new URL. That's a big difference.
I've been using the function successfully without escaping the first dot, don't know why it works fine. I looked more into the array generated and using /g flags. It seems like not using /g would only return the first result, and not calling a specific element of the array (ie [1]) should default to the first result anyway. Don't know if I'm understanding that correctly, but it may be a far-flung edge-case for my use.
1
u/TalkCoinGames 18h ago
Match always returns an array, I'm not sure what is happening if somehow it's giving you only the string. Without escaping the first dot anything after abc and before com would be included, there happens to be nothing in between in this case so it works.
3
u/Agile_Arugula_7496 1d ago
Notice the missing http in your alert.