简体   繁体   English

我可以通过 iframe 从缩短的 URL 中获取参数吗?

[英]Can I get a parameter from shortened URL through an Iframe?

I have shortened URL's scanned from QRCODES.我已经缩短了从 QRCODES 扫描的 URL。 The full URL has some parameters that I need.完整的 URL 有一些我需要的参数。

I'm putting up an Iframe to load the shortened URL and was hoping to get the "full" url once it loaded.我正在建立一个 iframe 来加载缩短的 URL,并希望在加载后获得“完整”的 url。

But didn't get to work...但是没上班...

Is it possible?可能吗?

Once the Iframe is loaded, I try access it using:加载 iframe 后,我尝试使用以下方式访问它:

iframe.contentWindow.location.href

But always get an error但是总是报错

ERROR DOMException: Blocked a frame with origin "http://localhost:8100" from accessing a cross-origin frame.

There are solutions to the communication between iframe and host if you control the content of both windows, but they often feel like hacks and can be limiting.如果您控制两个窗口的内容,则存在 iframe 和主机之间通信的解决方案,但它们通常感觉像是 hack 并且可能会受到限制。

For a general solution, you could get the redirected URL before you even load the iframe.对于一般解决方案,您甚至可以在加载 iframe 之前获取重定向的 URL。

The fetch api has a "redirect" option you can set to "manual" to get the URL it redirects to. fetch api 有一个“重定向”选项,您可以将其设置为“手动”以获取它重定向到的 URL。

fetch(shorturl, {redirect: "manual"})
   .then(function(response) {
       var iframeurl;
       if (response.type==="opaqueredirect") {
           // set to redirected url
           iframeurl = response.url
       }
       else {
           // use original url.
           iframeurl = shorturl
       }
       iframe.contentWindow.location.href = iframeurl;
       handleQueryString(iframeurl)


   })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM