Javascript 多窗口.onload函数
我有一个名为Javascript 多窗口.onload函数,javascript,javascript-events,Javascript,Javascript Events,我有一个名为start()的Javascript函数,可以使用window.onload函数加载多个函数。然而,我有下面的功能,独立工作良好。但是,如果我在窗口.onload中编写它,那么它就不起作用了 //START() window.onload = start; function start() { loadValues(); showState4(); } 独立工作的代码很好 window.onload=function(){ document.ge
start()
的Javascript函数,可以使用window.onload
函数加载多个函数。然而,我有下面的功能,独立工作良好。但是,如果我在窗口.onload
中编写它,那么它就不起作用了
//START()
window.onload = start;
function start()
{
loadValues();
showState4();
}
独立工作的代码很好
window.onload=function(){
document.getElementById("src2TargetAll").onclick = function() {
sureTransfer(document.getElementById("net"), document.getElementById("target"), true);
};
};
我尝试在窗口中重新编写代码,如下所示。onload
但不起作用。如何在单个窗口中重新写入以下代码。onload
函数
window.onload = start;
function start()
{
loadValues(); //loadValues() and showState4() works fine without sendValues().
showState4();
sendValuess(); // tested this sendValues without above two functions and that also works fine. but three functions in window.onload creates a problem
}
function sendValuess(){
document.getElementById("src2TargetAll").onclick = function() {
sureTransfer(document.getElementById("net"), document.getElementById("target"), true);
};
};
将sendValues()
添加到窗口后出现的错误。onload
如下所示:
STOP RUNNING THIS SCRIPT?
A SCRIPT ON THIS PAGE IS CAUSING YOUR WEB BROWSER TO RUN SLOWLY. IF IT CONTINUES TO RUN, YOUR COMPUTER MIGHT BECOME UNRESPONSIVE.
以下是试图帮助我的人员请求的loadValues和其他函数的代码:
function showState4(){
var me = document.getElementById('stk1');
var values = ''; //populate selected options
for (var i=0; i<me.length; i++)
if (me.options[i].selected)
values += me.options[i].value + ',';
values = values.substring(0, values.length-1);
var selected=[values];
var temp= new Array();
temp = values.split(",");
var del = document.getElementById('StakeHolder');
for(var i=0; i<del.length; i++)
{
for(var j=0;j<temp.length;j++)
{
if(temp[j] == del.options[i].value)
{
del.options[i].selected = true;
}
}
}
}
function loadValues()
{
var RD_REQ_RT_ID = "<%=RD_REQ_RT_ID %>";
if(RD_REQ_RT_ID=="null")
{
document.getElementById('requestType').value="";
}
else{
document.getElementById('requestType').value=RD_REQ_RT_ID;
}
)
_
function sureTransfer(from, to, all) {
if ( from.getElementsByTagName && to.appendChild ) {
while ( getCount(from, !all) > 0 ) {
transfer(from, to, all);
}
}
}
function getCount(target, isSelected) {
var options = target.getElementsByTagName("option");
if ( !isSelected ) {
return options.length;
}
var count = 0;
for ( i = 0; i < options.length; i++ ) {
if ( isSelected && options[i].selected ) {
count++;
}
}
return count;
}
function transfer(from, to, all) {
if ( from.getElementsByTagName && to.appendChild ) {
var options = from.getElementsByTagName("option");
for ( i = 0; i < options.length; i++ ) {
if ( all ) {
to.appendChild(options[i]);
} else {
if ( options[i].selected ) {
to.appendChild(options[i]);
}
}
}
}
}
函数showState4(){
var me=document.getElementById('stk1');
变量值=“”;//填充所选选项
对于(var i=0;i误差:
STOP RUNNING THIS SCRIPT?
A SCRIPT ON THIS PAGE IS CAUSING YOUR WEB BROWSER TO RUN SLOWLY.
IF IT CONTINUES TO RUN, YOUR COMPUTER MIGHT BECOME UNRESPONSIVE.
当IE在兼容模式下运行时发生。(在使用FB和Google+时发生)
转到“工具”菜单并选择“兼容性视图设置”选项。这将打开一个窗口,您可以在其中关闭“使用兼容性视图显示所有网站”功能.为loadValues
、showState4
和sureTransfer
共享您的代码。您可以使用jQuery吗?@ATOzTOA:-我更新了代码。我想这篇文章可以帮助您:)Marawan,我已经这么做了。问题只是在添加第三个时出现的。前两个可以正常工作,没有任何问题。您遇到了什么错误?检查控制台。在mozilla ctrl+shift+j@Mayank中:控制台没有错误。我弹出的错误如下:停止运行此脚本?此页面上的脚本正在导致您的WEB浏览器停止运行N缓慢。如果它继续运行,您的计算机可能会失去响应。您似乎正在使用“internet exploder”…尝试其他浏览器
window.addEventListener will not work in IE so use window.attachEvent
You can do something like this
function fun1(){
// do something
}
function fun2(){
// do something
}
var addFunctionOnWindowLoad = function(callback){
if(window.addEventListener){
window.addEventListener('load',callback,false);
}else{
window.attachEvent('onload',callback);
}
}
addFunctionOnWindowLoad(fun1);
addFunctionOnWindowLoad(fun2);