0

I have a website with a parent page (index.php) that loads all the child pages in a frame and keeps the URL static (so if I navigate throughout the site the URL will always be htps://localhost/myWebsite/).

Now when I directly go to a child page (htps://localhost/myWebsite/childPage.php) the page will load without style and the website will not be on index.php.

so I have coded a solution to this by redirecting all child Pages back to index.php page and loading the frame content based on a session variable I passed through via HTML5. once on the index.php page the session variable will be cleared and all is good.

The only problem with this is that I am hard-coding a URL for a condition check inside of a external .JS file which holds my procedures for redirecting, setting/clearing session variable. My boss says he doesn't want any hard-coding whatsoever so I am wondering if there is a way to grab the index.php page URL value, regardless of the page that you are currently on?

I need to have a variable which will always hold the value "htps://localhost/myWebsite/" (Main URL) and a variable that will always hold the value: "htps://localhost/myWebsite/index.php" (Home Page)

var theURL = window.location.protocol 
             + "//" 
             + window.location.hostname 
             + window.location.pathname;


var theURL = document.URL;

both of these work great for grabbing the current URL value but changes on different pages. I have tried using PHP to get the server root but it navigates me to an hierarchical view of the website.

Is there a way to do this? Or should I find a different solution entirely?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Altef four
  • 340
  • 5
  • 18
  • That just means the uris in your `` tags and whatnot to load the css are incorrect. You probably want something like `` (note the leading `/`), instead of just ``. There is no need for JS and whatnot to fix this. just output proper paths into your html to start with. – Marc B May 02 '14 at 18:57
  • There are no tags issues anywhere in the website. It is all Javascript and PHP with very minimal HTML mark up. You are right about the "/" thanks I didn't notice that. but it doesn't solve my issue unfortunately. – Altef four May 02 '14 at 19:18
  • This seems like an xy problem. What are you actually trying to accomplish here? You said the page loads without styles which implies that you're using CSS to style the page. If this is the case then in order to have your styles applied correctly you need to add tags with proper paths like Marc B said. – doliver May 02 '14 at 19:22
  • No styles will load because the child page has no styles on it. the styles are handled on the parent page, which loads the child pages on it and then the styles. This works already. I was just explaining what happens when you manually navigate to said page. I said i have coded a solution to this... the only problem is that I have to hardcode a value to get it to work. I want to figure out how to populate the variable without hardcoding. Sorry for not being specific enough. – Altef four May 02 '14 at 19:28

1 Answers1

1

There's nothing particularly special about index.php that would allow you to find it via javascript. You could use php to search for 'index.php' under your website root but this would be totally redundant since you already know where it is. "Hard coding" here does not really mean what you think it means. Generally, the way that you handle this kind of situation is by defining a variable (in php a constant) to hold the value, e.g.

var REDIRECT="/myWebsite/index.php"

and then in place of using the absolute redirect use the variable value. That way, if it needs to change in the future you can change it in one place. This is really more a question of what can logically be done than what you can do with javascript. You cannot redirect to, nor can you calculate the path of an arbitrary destination. Said path must be calculated in some way and in your case the calculation is trivial - you already know what it is.

Note that here, I used a / and omitted the full website url. That way if you have to move the site from your local machine to a live version your redirect should still work.

Here: How to extract base URL from a string in JavaScript? is a question that addresses calculating the base path for a url which you might have needed in a different kind of case.

Community
  • 1
  • 1
doliver
  • 980
  • 6
  • 7