如何在变量中存储包含哈希标记的完整 url? [英] How can I store a full url that contains a hashmark within a variable?

查看:31
本文介绍了如何在变量中存储包含哈希标记的完整 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试存储一个网址,例如:

I'm trying to store a url such as:

http://localhost/pro_print/index.php#page=home

在一个变量中,但我找不到执行此操作的函数.我找到了很多存储 index.php 的解决方案,但它不包含哈希标记及其后的内容.有没有针对此的 PHP 解决方案?

in a variable, but I can't find a function that does that. I found plenty of solutions to store the index.php, but it doesn't include the hashmark and what follows it. Is there a PHP solution for this?

我确实看到我可以使用 document.write(document.url)document.write(location.href) 获得包含带有 javaScript 的哈希标记的完整 url,但是我如何将它存储到我的变量中?有什么办法可以在这样的解决方案中将 PHP 与 javaScript 结合起来吗?

I did see that I can get the full url including hashmark with javaScript using document.write(document.url) or document.write(location.href) but how do I store that into my variable? Is there any way I can combine PHP with javaScript in some sort of solution like this?

<?php $url ="?><script>document.write(document.url)</script><?php "?>

推荐答案

片段标识符(# 及其后面出现的所有内容)完全由客户端处理,并且不会在请求 URI 时发送到服务器.

The fragment identifier (the # and everything that appears after it) is handled entirely client side, and is not sent to the server when the URI is requested.

要使其可用于 PHP,您必须:

To make it available to PHP, you would have to:

  1. 允许页面加载
  2. 使用 JavaScript 读取位置
  3. 使用 Ajax 技术(例如 XMLHttpRequest)或在后续请求中将其发送到服务器

这不会在原始脚本运行时使其对服务器可用,但其他任何事情都不能.

This won't make it available to the server at the time the original script runs, but nothing else can.

另一种方法是将片段标识符中的信息复制到 URI 中的其他位置(例如查询字符串).本网站在提交答案时使用此名称.

An alternative approach would be to duplicate the information in the fragment identifier somewhere else in the URI (e.g. the query string). This is used by this site when submitting an answer.

这篇关于如何在变量中存储包含哈希标记的完整 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆