0

I am trying to redirect a page in JavaScript using a variable base_url. The issue is page redirects to the wrong URL rather than the one I want to redirect to.

The script is running on this page http://www.example.com/account

In my JS code, I have:

base_url = 'www.example.com';
window.location.href = base_url + '/payment';

How can I figure out how to send users to www.example.com/payment rather than http://www.example.com/www.example.com/payment?

I don't understand why my code is pre-pending the domain name in the URL? How can I solve this issue? Any ideas?

EDIT

OK guys, I changed my base url with

base_url = '//www.domainhole.com';

I am not sure whether the page will serve on http or https therefore I used protocol-relative URL.

But why without using scheme it makes wrong url?

asked Jul 18, 2018 at 18:04
3
  • What happens if you change it to base_url = 'http://www.example.com';? Commented Jul 18, 2018 at 18:06
  • Try using this window.location.replace = base_url + '/payment'; Commented Jul 18, 2018 at 18:16
  • no, it loses history Commented Jul 18, 2018 at 18:17

1 Answer 1

3

Include a protocol in your base_url: base_url = 'https://www.example.com';

answered Jul 18, 2018 at 18:06
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.