简体   繁体   中英

How to get a URL value from another page JavaScript / PHP

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?

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, eg

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM