Jeremy Brooks
Jeremy Brooks

Reputation: 67

Javascript get url and redirect by adding to it

I have a situation where I need to get the current page url and redirect to a new page by adding to the current page url. For example:

http://www.mywebsite.com/page1/page2

needs to redirect to:

http://www.mywebsite.com/page1/page2/page3

I need it to be relative because "/page1/page2/" will always be different, but "page3" will always be the same.

I've tried: location.href = "./page3"; but that does not work. The result is: http://www.mywebsite.com/page1/page3

Any thoughts?

Upvotes: 0

Views: 87

Answers (2)

FalleStar
FalleStar

Reputation: 11

Get and Set URL using either window.location.href or document.URL;

window.location.href = window.location.href + "/page3";

Should do what you're looking for.

Upvotes: 0

Arg0n
Arg0n

Reputation: 8423

Maybe this?:

location.href = location.pathname + "/page3";

Upvotes: 3

Related Questions