javascript 旧代码:window.parent.location.href="https:/home"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18279097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:18:57  来源:igfitidea点击:

Legacy code: window.parent.location.href="https:/home"

javascriptredirectlocation-hrefwindow.parent

提问by Tom Hert

I have inherited some code from our former developer and I found this piece of code.

我从我们以前的开发人员那里继承了一些代码,我找到了这段代码。

<?php
   if($this->loginAction->isAuthenticated()){
?>
   <script type="text/javascript">
      window.parent.location.href="https:/home";
   </script>
<?php
}
?>

I have been looking on it for a while and I have no idea why he put there the shortcut for the URL. I think that the URL should be https://mysite.com/home. I basically don't know what does this code do.

我一直在寻找它一段时间,我不知道他为什么把 URL 的快捷方式放在那里。我认为 URL 应该是https://mysite.com/home。我基本上不知道这段代码是做什么的。

Edit:I know that this code is redirection if user is authenticated, but I don't know what the strange URL(https:/home) is doing there. This code is working.

编辑:我知道如果用户通过身份验证,此代码是重定向,但我不知道奇怪的 URL(https:/home) 在那里做什么。此代码正在工作。

Thank you for any help.

感谢您的任何帮助。

Edit2:According to my colleague, this is one of the JS secrets:) Going to test it. This two lines of code should have equal functionality:

Edit2:据我的同事说,这是 JS 的秘密之一:) 去测试一下。这两行代码应该具有相同的功能:

window.parent.location.href="https:/home";
window.parent.location.href="https://www.my-testing.com/home";

采纳答案by Mark Schultheiss

This is the URI and it is valid according to the spec here: https://www.ietf.org/rfc/rfc2396.txtin section 3 as quoted here: ( the bold and highlights are mine) This says that the scheme httpsfollowed by the separator colon :then the rest is optional. Thus the single forward slash is likely interpreted as the root or "full" URI of "https://home" or simply the "/home" of the current provider. Likely this then looks like "https://www.example.com/home" if your site is www.example.com (it isn't but that one is in the specifications as a reserved name as well) which is the DNS address of your site. Note that if the scheme were missing as "//www.ourbug.net" then that would be translated into the current scheme in use (http or https for most resources)

这是 URI,根据此处的规范,它是有效的:https: //www.ietf.org/rfc/rfc2396.txt在第 3 节中引用如下:(粗体和突出显示是我的)这表示方案https后跟分隔符冒号然后其余部分是可选的。因此,单个正斜杠可能被解释为“ https://home”的根或“完整”URI,或者只是当前提供者的“/home”。可能这看起来像“ https://www.example.com/home" 如果您的站点是 www.example.com(它不是但在规范中也作为保留名称)这是您站点的 DNS 地址。请注意,如果该方案丢失为“//www .ourbug.ne​​t”,然后将其转换为当前使用的方案(大多数资源为 http 或 https)

Quote from the reference:

引用自参考:

3. URI Syntactic Components

3. URI 语法组件

The URI syntax is dependent upon the scheme. In general, absolute URI are written as follows:

URI 语法取决于方案。一般来说,绝对URI的写法如下:

  <scheme>:<scheme-specific-part>

An absolute URI contains the name of the scheme being used () followed by a colon (":") and then a string (the ) whose interpretation depends on the scheme.

绝对 URI 包含正在使用的方案的名称 () 后跟一个冒号 (":"),然后是一个字符串 (the),其解释取决于方案。

The URI syntax does not require that the scheme-specific-part have any general structure or set of semantics which is common among all URI. However, a subset of URI do share a common syntax for representing hierarchical relationships within the namespace. This "generic URI" syntax consists of a sequence of four main components:

URI 语法不要求方案特定部分具有所有 URI 中通用的任何通用结构或语义集。然而,URI 的一个子集共享一个通用的语法来表示命名空间内的层次关系。 这种“通用 URI”语法由四个主要部分组成:

  <scheme>://<authority><path>?<query>

each of which, except , may be absent from a particular URI.For example, some URI schemes do not allow an component, and others do not use a component.

除了 ,每个都可能不存在于特定的 URI 中。例如,某些 URI 方案不允许使用组件,而另一些则不使用组件。

  absoluteURI   = scheme ":" ( hier_part | opaque_part )

URI that are hierarchical in nature use the slash "/" character for separating hierarchical components. For some file systems, a "/" character (used to denote the hierarchical structure of a URI) is the delimiter used to construct a file name hierarchy, and thus the URI path will look similar to a file pathname. This does NOT imply that the resource is a file or that the URI maps to an actual filesystem pathname.

本质上是分层的 URI 使用斜杠“/”字符来分隔分层组件。对于某些文件系统,“/”字符(用于表示 URI 的层次结构)是用于构建文件名层次结构的分隔符,因此 URI 路径看起来类似于文件路径名。这并不意味着资源是文件或 URI 映射到实际文件系统路径名。

  hier_part     = ( net_path | abs_path ) [ "?" query ]

  net_path      = "//" authority [ abs_path ]

  abs_path      = "/"  path_segments"

回答by warpedspeed

Well, it appears to say if the login action is authenticated then redirect the user to a new url.

好吧,似乎是说如果登录操作经过身份验证,则将用户重定向到新的 url。

Seems like a bug to me. I don't believe https:/home is anything more than a typo.

对我来说似乎是一个错误。我不相信 https:/home 只是一个错字。

回答by Raja Manickam

Try:

尝试:

<script type=text/javascript>
   window.parent.location="https://www.my-testing.com/home";
</script>