Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 页面加载前执行的脚本_Javascript_Html_Iframe - Fatal编程技术网

Javascript 页面加载前执行的脚本

Javascript 页面加载前执行的脚本,javascript,html,iframe,Javascript,Html,Iframe,我已经完成了一个脚本,它运行在一个页面上,该页面包含一个电子邮件发送的文本区域 我在这个页面上做了很多事情,但是其中之一就是根据所选的数字加载一个iframe,然后一旦加载iframe,就从这个页面获取我需要的相关细节 我已经在一个名为frameLoaded的函数中为此编写了代码,并将其设置为onload事件,但是脚本仍然会遇到一个错误,无法找到元素的.innerHTML 如果我加载iframe然后执行这个脚本,它工作得很好,但是如果我尝试加载iframe并同时执行这个脚本,那么它就会出现这个错

我已经完成了一个脚本,它运行在一个页面上,该页面包含一个电子邮件发送的文本区域

我在这个页面上做了很多事情,但是其中之一就是根据所选的数字加载一个iframe,然后一旦加载iframe,就从这个页面获取我需要的相关细节

我已经在一个名为frameLoaded的函数中为此编写了代码,并将其设置为onload事件,但是脚本仍然会遇到一个错误,无法找到元素的.innerHTML

如果我加载iframe然后执行这个脚本,它工作得很好,但是如果我尝试加载iframe并同时执行这个脚本,那么它就会出现这个错误

以下是我使用的代码:

//Getting text currently in the textarea
var selectedTxt = document.getElementById('txtEmailText').value;

//Converting it to a string - this is just for troubleshooting purposes that I've used two variables
var insertText = selectedTxt.toString();

//Loads in the highlighted purchase number
var purchaseNumber = window.getSelection();
purchaseNumber = purchaseNumber.toString();

//Declares global variables to hold the title and number
var purchaseTitle;
var purchaseNumber;

//Function to execute code to grab title and number once the frame has loaded
function frameLoaded() {
var iframe = document.getElementById('purchaseIframe');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
          purchaseTitle = innerDoc.getElementById('listingTitle');
          purchaseNumber = innerDoc.getElementById('auctionSoldIdDisplay');
         }  


//Checks to see if a purchase number has been selected 
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)); 
}

//Converting the value in the title to readable text
purchaseTitle = purchaseTitle.innerHTML;

//Placing the values in to the format I need
var purchaseDetails = purchaseTitle + " - " + purchaseNumber;

//Placing the values in to the string to go back in to the email textarea
insertText = insertText.replace("PURCHASEDETAILS", purchaseDetails);

//Pasting the variable in to the textarea
document.getElementById('txtEmailText').value = insertText;
我在这里做错了什么,或者使用了错误的事件,因为在我看来,它可能试图在iframe完全加载之前获取值,因此在同时生成iframe时出错

请注意,我不能使用JQuery

窗口。onload=function()是您的答案

注意,在下面的示例中,document.getElementById('mydiv')的第一个实例返回null,因为页面(DOM)尚未加载,因此div也没有加载

第二个实例在windows.onload上启动,这意味着所有页面内容都存在,因此可以找到

<script type='text/javascript'>
console.log("Page not ready:" + document.getElementById('mydiv'));
window.onload = function() {
  console.log("Page ready:" + document.getElementById('mydiv'));
}
</script>

<div id="mydiv">

</div>

log(“页面未就绪:+document.getElementById('mydiv'));
window.onload=函数(){
log(“页面就绪:+document.getElementById('mydiv'));
}

您是否尝试过
purchaseIframe.setAttribute(“onload”、“frameLoaded”)
而不是
purchaseIframe.setAttribute(“onload”,“frameLoaded();”?如果这听起来很愚蠢,很抱歉,但我应该将其放在代码中的什么位置?我试着把它放在函数
window.onload=function frameLoaded
purchaseIframe.setAttribute(“window.onload”,“frameLoaded();”)旁边但我不太确定该怎么办,因为我没有权限在iframe页面中编辑代码。实际上,您希望在onload函数中运行代码。或者,您可以执行类似window.onload=init()的操作;函数init(){//all your stuff}只是澄清一下,我没有访问iframe中的代码来添加代码。那么,如何将其添加到加载时在iframe上执行的脚本中呢?purchaseIframe.onload=function(){this.document.getElementById('SomeElementInsideTheFrame);}我还没有通过设置函数frameLoaded,然后在此处将其设置为属性来使用它:
purchaseIframe.setAttribute(“onload”,“frameLoaded();”