【问题标题】:Redirect Window from iFrame从 iFrame 重定向窗口
【发布时间】:2023-04-02 22:23:01
【问题描述】:

我希望有人可以帮助解决我目前遇到的问题。我在客户网站上实现了一个名为 IFrame1IFrame。在客户网站上,他们在QueryString 中拥有用户的某些信息,例如clientwebsite.com/login.aspx?UserID=xxxx&UserName=xxxx

使用MVC 在网站上只有IFrame 时检索整个查询字符串的最佳方法是什么?

我尝试使用JavaScript 来检索window.location,但这仅显示我的IFrame 部署的URL (MyServer.com),而不是它的实施位置(clientwebsite.com)。我还尝试使用 C#HttpRequest.QueryString。这也只会显示部署我的解决方案的 URL,而不是客户端网站 URL。

如果有人有任何建议,将不胜感激。

编辑

我尝试按照许多人的建议使用window.parent.location。我写了一个小网页,我只是拉代码并显示如下:

<script type="text/javascript">
    document.write("window.location.href : " + window.location.href + "<br />");
    document.write("window.parent.location : " + window.parent.location + "<br />");
    document.write("document.referrer : " + document.referrer + "<br />");
</script>

当我运行这个页面时,我得到如下结果:

window.location.href http://mywebsite.com/test
window.parent.location http://mywebsite.com/test
document.referrer 

这很好,但是当我通过客户端网站查看它时,它只显示 href 值,而不显示 window.parent.location 标签或值。这可能是由于权限引起的吗?

【问题讨论】:

  • 尝试document.referrer获取父母的网址
  • 尝试使用window.parent.location获取父级的url

标签: javascript c# asp.net iframe


【解决方案1】:

您应该尝试使用:

var parentUrl = window.parent.location;

window.parent 会让你进入父窗口,然后你可以对父窗口做任何事情。

嗨鲁本,

我相信你需要添加 window.parent.location.search 。您可以对查询字符串执行类似的操作。这个答案由 Marwan 提供:

function getQueryString() {
            var queryStringKeyValue = window.parent.location.search.replace('?', '').split('&');
            var qsJsonObject = {};
            if (queryStringKeyValue != '') {
                for (i = 0; i < queryStringKeyValue.length; i++) {
                    qsJsonObject[queryStringKeyValue[i].split('=')[0]] = queryStringKeyValue[i].split('=')[1];
                }
            }
            return qsJsonObject;
        }

只需像这样从子窗口中调用它,并将查询字符串作为对象进行操作。

例如,如果您有查询字符串 ?name=stack 并且想要获取它,请尝试:

getQueryString().name

这将返回堆栈。

【讨论】:

  • 我将此添加到我的编辑部分。请看看发生了什么,如果你能想到为什么会发生这种情况。
  • 嗨@RubenRedman,我已经编辑了上面的答案。有看。现在应该很好了。以上所有代码都应该在 iframe 中。
猜你喜欢
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多