dqnrk44682 2014-05-02 18:55
浏览 21
已采纳

如何从另一个页面JavaScript / PHP获取URL值

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?

  • 写回答

1条回答 默认 最新

  • douyao4632 2014-05-02 19:49
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?