简体   繁体   English

Windows onload无法从外部脚本触发

[英]windows onload not firing from external script

Okay i know this has been asked a lot but it seems that none of the solutions actually work for me. 好的,我知道这个问题已经被问了很多,但似乎没有一种解决方案真正适合我。 Here's the situation. 这是情况。 I have a webpage that i need to add to an existing site, this site uses a master page which i can not touch. 我有一个需要添加到现有网站的网页,该网站使用的母版页面我无法触摸。 This limits me to using javascripts window.onload because i do not have access to the body tag. 这限制了我使用javascripts window.onload的权限,因为我无权访问body标签。

On my page i have linked my external .js file in the head beneath every other external file. 在我的页面上,我已将我的外部.js文件链接在所有其他外部文件下面的头部中。 Here is an example of what my .js file looks like. 这是我的.js文件外观的示例。

var myobj = null;
(5 or so functions that work properly. Mainly just toggles that show and hide divs. none touch the onload)
function load(){
myobj = document.getElementById("my_element");
alert("test");
}
window.onload = load;

Tried this and the load function never fires as i never get the alert. 尝试了这一点,并且加载功能从未触发,因为我从未收到警报。 I've tried commenting out the first line in the load function and only haven't the alert and still nothing. 我尝试注释掉load函数的第一行,但没有警告,仍然一无所获。 Looking around i also found another way to do it that i tried without success. 环顾四周,我还发现了另一种尝试失败的方法。 Everything else is the same except i removed the window.load and added this. 除我删除window.load并添加此内容外,其他所有内容均相同。

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
    window.onload = func;
} else {
    window.onload = function () {
        if (oldonload) {
            oldonload();
        }
        func();
    }


  }
}
addLoadEvent(load);
addLoadEvent(function () {
    /* more code to run on page load */
    alert("hello2");
});

In this function, neither of the alerts fire. 在此功能中,不会触发任何警报。 I have also tried placing an alert outside of a function, that one does work. 我还尝试过在功能之外放置一个警报,该警报确实起作用。

The browsers i am using are IE 8.0.7600.16385 and Chrome 21.0.1180.60 我正在使用的浏览器是IE 8.0.7600.16385和Chrome 21.0.1180.60

Let me know if you need more information. 如果您需要更多信息,请与我们联系。

Oh and as a side note, i cannot use jquery or any other javascript library as we are trying to keep this as light as possible. 哦,作为补充,我不能使用jquery或任何其他javascript库,因为我们正努力使它尽可能轻。 This page also must work in IE8, that is the businesses only supported browser at the moment. 此页面也必须在IE8中工作,这是当前仅支持企业的浏览器。 It would be nice if it worked in Chrome as well. 如果它也适用于Chrome,那将是很好的。

EDIT In case i'm going about this completely the wrong way, what i am trying to do is keep track of the last element i have. 编辑以防万一我完全以错误的方式进行操作,我想做的是跟踪我拥有的最后一个元素。 I essentially have a page with 2 columns, left side is a menu and right is content. 我基本上有一个包含2列的页面,左侧是菜单,右侧是内容。 The content is all placed in div's with only one category showing at a time. 内容全部放在div中,一次仅显示一个类别。 The way it is supposed to work is when clicking on the menu category it hides the previous content and shows the new content. 它的工作方式是单击菜单类别时,它将隐藏先前的内容并显示新的内容。

I have set my content class to display: none; 我将内容类设置为显示:无; and have the start_content ID set to display: block;. 并将start_content ID设置为显示:block;。 What the Javascript is supposed to do is initialize my global with the start_content object. Javascript应该做的是使用start_content对象初始化我的全局变量。 When clicking in the menu it calls a function that does an obj.style.display = 'none' and then sets the new obj to display = 'block'. 在菜单中单击时,它将调用一个执行obj.style.display ='none'的函数,然后将新的obj设置为display ='block'。 It then takes the new obj and places it in my global variable to be changed on the next menu click. 然后,它将新的obj放入到我的全局变量中,以在下次单击菜单时进行更改。

here's an example without any of the onload functions 这是一个没有任何onload函数的示例

var prevContent = document.getElementById("start_content");
function toggle(id) {
prevContent.style.display = "none";
var content = document.getElementById(id);
content.style.display = "block";
prevContent = content;
}

The problem with this is that prevContent is unidentified when it enters the toggle function. 问题在于,prevContent进入切换功能时无法识别。 I had assumed this was because i am linking this .js file in the head and so the page hasn't loaded my start_content yet which is why i had changed it to declaring the global as a null and then setting up a window.onload to set the appropriate value after it is created. 我以为这是因为我将这个.js文件链接到头部,因此该页面尚未加载start_content,这就是为什么我将其更改为将global声明为null,然后将window.onload设置为在创建之后设置适当的值。

最后,将“ Defer”添加到脚本标签中就可以了。

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

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