Sachin
Sachin

Reputation: 1698

Page redirection in JavaScript using a variable

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?

Upvotes: 0

Views: 47

Answers (1)

Jeph
Jeph

Reputation: 465

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

Upvotes: 3

Related Questions