开发者

How to bind new functions for HTML Elements using jQuery?

First of all a little background of my problem, i'm trying to identify how the user is navigating through the site, and i got a script (from an stackoverflow user, thanks a lot) called autoLogoff.js, it works very well until the point i'm trying to submit a form using javascript. Here's the autoLogoff code (with some modifications i'm trying to do)

function endSession() {
 lo开发者_StackOverflowcation.replace('/login/');
}

var validNavigation = false;

function wireUpEvents() {
 // Attach the event click for all links in the page
 $("a").bind("click", function() {
   validNavigation = true;
 });

 // Attach the event submit for all forms in the page
 $("form").bind("submit", function() {
  validNavigation = true;
 });

 // Attach the event submit for all submit buttons in the page
 $("input:submit").bind("click", function() {
  validNavigation = true;
 });

 // Attach the event submit for all submit buttons in the page
 $("input:button").bind("click", function() {
  validNavigation = true;
 });

 $("form").manualSubmit = function(){
  validNavigation = true;
  this.submit();
 };
}

// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
    wireUpEvents();
    /*
 * For a list of events that triggers onbeforeunload on IE
 * check http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx
 */
 window.onbeforeunload = function() {
  if (!validNavigation) {
   endSession();
  }
 }
});

Now my problem is that on internet explorer when i do this on every javascript function

$('form').get(0).submit();

The variable (validNavigation) is never set, therefore sending me to the login and killing the session, which is not desireable, neither i want to manually set the variable on every function so i came up with the idea to add a custom submit function to every form like you see on the code above. But whenever i do $('form').get(0).manualSubmit(); i get error that the function is not defined. Is anything wrong?

Thanks in advance PD: Sorry for the huge explanation but i thought it was necesary)


It looks like it's supposed to be $('form').manualSubmit();, but this:

 $("form").manualSubmit = function(){

is nonsense. It creates a new jQuery wrapper on the current list of forms, then adds a method to that wrapper, which is then discarded.

Probably what the author meant was:

jQuery.fn.manualSubmit= function() {
    validNavigation = true;
    this[0].submit();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜