简体   繁体   English

使用jQuery / javascript从div中加载的外部网页获取URL

[英]Get url from external webpage loaded in a div with jquery/javascript

I have a webpage, we will call it web1. 我有一个网页,我们将其称为web1。 This webpage has a div that contains an external webpage that will be web2. 该网页的div包含一个外部网页,该网页将为web2。 web2 is stored at our server so we have full control of it, so is web1. web2存储在我们的服务器上,因此我们可以完全控制它,web1也是如此。

The code at web1 to create the div that will contain web2 is as follow: web1上用于创建将包含web2的div的代码如下:

<div class="form-group">                
    <label class='control-label col-sm-1'>Design Report Searcher</label>
    <div class="col-sm-11">
        <div id="load-external"></div>
        </div>     
     </div>
</div>

And the jquery function to load the web2 和jquery函数加载web2

$("#load-external").html('<object style="height:320px; width:100%;" data="https://external.my.company.com/" />');

Web2 contains a list of documents stored at a server. Web2包含存储在服务器上的文档列表。

The user will navigate in web2 and once he founds the document he wants, web1 will need the url to parse it. 用户将在web2中导航,一旦找到所需文档,web1将需要URL来对其进行解析。

Is it possible at all to get that url using jquery? 是否有可能使用jquery来获取该URL?

http is a stateless protocol, every request is separated and you don't have access to previous states unless you keep that informations http是一个无状态协议,每个请求都是分开的,除非保留这些信息,否则您将无法访问以前的状态

you can use cookie to save web1 url and use it when you have done with web2 or if its possible pass web1 url to web2 url by url query like: 您可以使用cookie来保存web1网址,并在使用完web2或可能的情况下使用它(如果可能)通过url查询将web1网址传递给web2网址,例如:

web2.html?source=web1.html

or open web2 in iframe, indeed you're in both. 或在iframe中打开web2,的确是两者兼有。

After some research I made it work. 经过一番研究,我成功了。

First I changed the for an . 首先,我将更改为。 The main reason is that I found this question that helped me understand the difference between iframe and object tags. 主要原因是我发现了这个问题 ,可以帮助我了解iframe和对象标签之间的区别。

Then I used the following code to retrieve the current URL that is loaded at the iframe tag and display it at a textarea that will be send on the form to store it: 然后,我使用以下代码检索了在iframe标记中加载的当前URL,并将其显示在要在表单上发送以存储它的文本区域中:

$('#report-textarea').val(document.getElementById('iframe-report').contentWindow.location.href);

It's important to understand that I can do this because both webpages are in the same domain. 重要的是要了解我可以执行此操作,因为两个网页都在同一个域中。 For security reasons, you won't be able to accomplish it if it is in a different domain. 出于安全原因,如果它在其他域中,则将无法完成它。

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

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