简体   繁体   English

从同一域的iframe中的元素中获取值

[英]Fetching values from an element in an iframe on the same domain

I'm trying to condense two processes down in to one by having the two pages I need on one page using an iframe. 我正在尝试通过使用iframe将我需要的两个页面压缩在一个页面上,从而将两个过程压缩为一个。

I have a page that contains a text area (used for sending an email) and then I have a purchase reference page that contains the details of someones purchase. 我有一个包含文本区域的页面(用于发送电子邮件),然后有一个购买参考页面,其中包含某人的购买详细信息。

I'm trying to append an iframe of the purchase page to the bottom of my email page and then grab some data that's on it and insert it in to the text area. 我正在尝试将购买页面的iframe附加到电子邮件页面的底部,然后获取其中的一些数据并将其插入到文本区域中。

EDIT : This is what I have so far: 编辑 :这是我到目前为止:

Script one 剧本一

//Grabs the selected purchase number
var purchaseNumber = window.getSelection();
purchaseNumber = purchaseNumber.toString();

var purchaseTitle;
var purchaseNumber;

function frameLoaded() {
           purchaseTitle = window.frames['purchaseIframe'].contentDocument.getElementById ('listingTitle');
           purchaseNumber = window.frames['purchaseIframe'].contentDocument.getElementById ('auctionSoldIdDisplay');
           purchaseTitle = purchaseTitle.innerHTML;
           purchaseNumber = purchaseNumber.innerHTML

var purchaseDetails = purchaseTitle + " - " + purchaseNumber;

insertText = insertText.replace("PURCHASEDETAILS", purchaseDetails);
         }  

if(purchaseNumber.length > 0){

var purchaseIframe = document.createElement('iframe');
purchaseIframe.src = 'http://www.mysite.co.nz/Admin/Listing/PurchaseDisplay.aspx?asid=' + purchaseNumber + '&submit1=++GO++';
purchaseIframe.setAttribute("height","1000");
purchaseIframe.setAttribute("width","100%");
purchaseIframe.setAttribute("id","purchaseIframe");
purchaseIframe.setAttribute("onload", "frameLoaded();");
void(document.body.appendChild(purchaseIframe));
alert(purchaseNumber); 
}

Script Two 剧本二

//Gather the selected template
var selectedTxt = document.getElementById('txtEmailText').value;

//Change the selected txt to a string
var insertText = selectedTxt.toString();

var purchaseTitle = window.frames['purchaseIframe'].contentDocument.getElementById ('listingTitle');
var purchaseNumber = window.frames['purchaseIframe'].contentDocument.getElementById ('auctionSoldIdDisplay');

purchaseTitle = purchaseTitle.innerHTML;
purchaseNumber = purchaseNumber.innerHTML

var purchaseDetails = purchaseTitle + " - " + purchaseNumber;

insertText = insertText.replace("PURCHASEDETAILS", purchaseDetails);


//Pasting the variable in to the textarea
document.getElementById('txtEmailText').value = insertText;

Effectively I am highlighting the purchase reference number on the page then executing this script to open the purchase page using the highlighted number. 实际上,我在页面上突出显示了购买参考号,然后执行此脚本以使用突出显示的数字打开购买页面。 I am then grabbing the text values of the elements I need and pasting them in to the text area. 然后,我获取所需元素的文本值并将其粘贴到文本区域中。

I'm still pretty new to javascript and am teaching myself as I go. 我对javascript还是很陌生,并且我正在自学。

If i run the above scripts one after the other then it works like a charm, however if I try to run them together with the second in an onload() function set to the iframe then it won't. 如果我一个接一个地运行上述脚本,那么它就像一个符咒,但是,如果我尝试在设置为iframe的onload()函数中将它们与第二个脚本一起运行,则不会。

Any help would be greatly appreciated or if you could point me in the direction of an article to help. 任何帮助将不胜感激,或者如果您可以向我指出帮助的文章。

My first thought is that the iframe is not fully loaded before you try to get the values from it. 我首先想到的是,在尝试从iframe中获取值之前,iframe并未完全加载。 My thought would be to try adding an onload event to your iframe and then when it loads invoke a function that grabs the value. 我的想法是尝试在您的iframe中添加onload事件,然后在加载时调用获取该值的函数。

I would add purchaseIframe.setAttribute("onload", "frameLoaded();"); 我会添加purchaseIframe.setAttribute("onload", "frameLoaded();"); to your purchaseIframe block and then add the frameLoaded() function to your script. 到您的PurchaseIframe块中,然后将frameLoaded()函数添加到脚本中。 something like: 就像是:

      function frameLoaded() {
            var purchaseTitle = window.frames[0].document.getElementById("listingTitle" );
            var purchaseNumber = window.frames[0].document.getElementById("auctionSoldIdDisplay");
            console.log(purchaseTitle.innerHTML);
            console.log(purchaseNumber.innnerHTML);
         }  

And see if something like that grabs the right values. 看看是否有类似的东西可以抓住正确的价值。 If it does than you can plug it in where you need it. 如果可以,则可以将其插入所需的位置。

Am I understanding your problem correctly? 我能正确理解您的问题吗?

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

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