从 iFrame 中提取 url 变量

Extracting the url variable from an iFrame

本文关键字:url 变量 提取 iFrame      更新时间:2023-09-26

我正在使用运行中的 API 使用这些说明上传视频,问题是我必须将上传表单包含在 iframe 中,上传表单将上传视频,完成后它将重定向到另一个页面,视频 ID 作为 url 变量。

我正在尝试做的是在上传完成后从 iframe 获取该 url 变量。

下面是 iframe 示例:

<iframe width="650" scrolling="auto" height="220" frameborder="0" src="http://developer.longtailvideo.com/botr/demo/upload/"></iframe>

更新完成后,iframe 将重定向到:

<iframe width="650" scrolling="auto" height="220" frameborder="0" src="http://developer.longtailvideo.com/botr/demo/upload/show.php?video_key=xxxxx"></iframe>

这样 xxxxx 是我想在父页面中提取的视频密钥,我将在隐藏输入中使用此视频密钥以提交另一个表单。

当重定向发生时,我是否可以获取此 url 变量?

试试这个:

ifr=document.getElementById('Your_Iframe_Id');
ifr.contentWindow.onload=function (){
  ssearch=ifr.contentWindow.location.search; // = ?video_key=xxxxx
  /* do something with ssearch */
  return;
}

在这种情况下,计时非常重要,因为ifr.contentWindow在首次加载之前不存在。我建议您首先将页面从服务器加载到IFRAME,然后将该页面的onload事件处理程序分配给主机页面。

或者,您可以等到加载整个主机页面(window.onload),然后为ifr.contentWindow分配事件处理程序。