admin管理员组文章数量:1023009
I need to attach a submit listener to every form in an iframe but I can't use jQuery.
What's the best way of doing this? I'd rather not use window.onload
because that requires waiting until the images have loaded which may take time. Obviously, $(document).ready()
would have been perfect, but as I said, I can't use jQuery for this.
Thanks
I need to attach a submit listener to every form in an iframe but I can't use jQuery.
What's the best way of doing this? I'd rather not use window.onload
because that requires waiting until the images have loaded which may take time. Obviously, $(document).ready()
would have been perfect, but as I said, I can't use jQuery for this.
Thanks
Share Improve this question asked Sep 22, 2011 at 11:54 Nick BruntNick Brunt 10.1k11 gold badges58 silver badges84 bronze badges2 Answers
Reset to default 5Onload will do the trick. Martin's jQuery approach will be maybe faster.
for(var i=0; i<document.forms.length; i++){
var form = document.forms[i];
form.addEventListener("submit", myListener,false);
}
Just roll your own :) here is the code that jQuery uses to have the ready event, take what you need, it's free (but do mention in your code where it came from ;)
bindReady: function() {
if ( readyList ) {
return;
}
readyList = jQuery._Deferred();
// Catch cases where $(document).ready() is called after the
// browser event has already occurred.
if ( document.readyState === "plete" ) {
// Handle it asynchronously to allow scripts the opportunity to delay ready
return setTimeout( jQuery.ready, 1 );
}
// Mozilla, Opera and webkit nightlies currently support this event
if ( document.addEventListener ) {
// Use the handy event callback
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
// A fallback to window.onload, that will always work
window.addEventListener( "load", jQuery.ready, false );
// If IE event model is used
} else if ( document.attachEvent ) {
// ensure firing before onload,
// maybe late but safe also for iframes
document.attachEvent( "onreadystatechange", DOMContentLoaded );
// A fallback to window.onload, that will always work
window.attachEvent( "onload", jQuery.ready );
// If IE and not a frame
// continually check to see if the document is ready
var toplevel = false;
try {
toplevel = window.frameElement == null;
} catch(e) {}
if ( document.documentElement.doScroll && toplevel ) {
doScrollCheck();
}
}
},
I need to attach a submit listener to every form in an iframe but I can't use jQuery.
What's the best way of doing this? I'd rather not use window.onload
because that requires waiting until the images have loaded which may take time. Obviously, $(document).ready()
would have been perfect, but as I said, I can't use jQuery for this.
Thanks
I need to attach a submit listener to every form in an iframe but I can't use jQuery.
What's the best way of doing this? I'd rather not use window.onload
because that requires waiting until the images have loaded which may take time. Obviously, $(document).ready()
would have been perfect, but as I said, I can't use jQuery for this.
Thanks
Share Improve this question asked Sep 22, 2011 at 11:54 Nick BruntNick Brunt 10.1k11 gold badges58 silver badges84 bronze badges2 Answers
Reset to default 5Onload will do the trick. Martin's jQuery approach will be maybe faster.
for(var i=0; i<document.forms.length; i++){
var form = document.forms[i];
form.addEventListener("submit", myListener,false);
}
Just roll your own :) here is the code that jQuery uses to have the ready event, take what you need, it's free (but do mention in your code where it came from ;)
bindReady: function() {
if ( readyList ) {
return;
}
readyList = jQuery._Deferred();
// Catch cases where $(document).ready() is called after the
// browser event has already occurred.
if ( document.readyState === "plete" ) {
// Handle it asynchronously to allow scripts the opportunity to delay ready
return setTimeout( jQuery.ready, 1 );
}
// Mozilla, Opera and webkit nightlies currently support this event
if ( document.addEventListener ) {
// Use the handy event callback
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
// A fallback to window.onload, that will always work
window.addEventListener( "load", jQuery.ready, false );
// If IE event model is used
} else if ( document.attachEvent ) {
// ensure firing before onload,
// maybe late but safe also for iframes
document.attachEvent( "onreadystatechange", DOMContentLoaded );
// A fallback to window.onload, that will always work
window.attachEvent( "onload", jQuery.ready );
// If IE and not a frame
// continually check to see if the document is ready
var toplevel = false;
try {
toplevel = window.frameElement == null;
} catch(e) {}
if ( document.documentElement.doScroll && toplevel ) {
doScrollCheck();
}
}
},
本文标签: javascriptHow to attach a submit listener to all forms without jQueryStack Overflow
版权声明:本文标题:javascript - How to attach a submit listener to all forms without jQuery - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745517422a2154137.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论