Dylan Cross
Dylan Cross

Reputation: 5986

javascript window location get path of website to redirect to between localhost and actual domain

I want to be able to redirect my user on logout of my website back to the homepage/login, this would be easy to do if I just redirected to the website itself, IE http://www.example.com/ however, how could I determine the root folder path for my site when it's on localhost?

for example, some of my websites are in: localhost/folder1/websiteBackup/ and some are in: localhost/

what would the best way to combine it so one line of code will work for both the localhost and the domain name?

Upvotes: 0

Views: 1840

Answers (4)

user704988
user704988

Reputation: 436

I would suggest use some server side session variables (PHP/ASP) and assign root path to them. then you can use them any time you want.

session("rootPath") = "/RootFolder/your-filename" (for ASP/VBScript)

Later use them in you JS code to redirect.

var path = '<%= session("rootPath")%> (JS code)';

Check similar question I asked in here for more info on Relative path, Relative Root path and Absolute root path.

Upvotes: 0

Boris Gappov
Boris Gappov

Reputation: 2493

document.location = 
    document.location.href.indexOf('localhost') >=0 ?
    document.location.href.split('localhost')[1] + 'index.htm' : 
    'index.htm';

Upvotes: 0

Scorpio
Scorpio

Reputation: 1171

this is what works for me..try your luck

  url:'<%=VirtualPathUtility.ToAbsolute("~/Include/ctnrls/Data.aspx") %>',

but I am mapping my virtual path in IIS

Upvotes: 0

Ry-
Ry-

Reputation: 224903

Use a relative path instead of an absolute one. For example, instead of

/somePage.html

you might write

../../somePage.html

If the script is shared and you don't know where it's going to be relative to the page, consider developing on separate <VirtualHost>s so that your environments match.

Upvotes: 2

Related Questions