admin管理员组

文章数量:1026989

I have this code

$(document).ready(function (){
    var Form_addTurbo = $('form#Form_addTurbo');
    Form_addTurbo.submit(function (e){
        e.preventDefault;
        v = new Notification('Creating Turbo.', 'information', 'Working..', function(){return;});
        $.post('/api/turbos/new.php', {
            action : 'createNew'
        }, function (r){
            v.hide();
            if(r.success){
                new Notification('Turbo Create.', 'saved', '', function(){return;});
            }else if(r.error){

            }else{
                new Notification('Something went wrong.', 'error', '', function(){return;});
            }
        }, 'json');
        return false;
    });
});

Which uses this api

$(document).ready(function(e) {$("body").prepend('<ul id="notifications"></ul>');});

/**
 * Global notification system
 *
 * @param  String      Message to be displayed
 * @param  String      Type of notification
 *
 * @author    Bram Jetten
 * @version    28-03-2011
 */
Notification.fn = Notification.prototype;

function Notification(value, type, tag, onclickfunc) {
  this.log(value, type);
  this.element = $('<li><span class="image '+ type +'"></span>' + value + '</li>');
  if(typeof tag !== "undefined" && tag !== '') {
    $(this.element).append('<span class="tag">' + tag + '</span>');
  }
  if(typeof onclickfunc == 'function'){
      this.element.click(onclickfunc);
  }
  $("#notifications").append(this.element);
  this.show();
}

/**
 * Show notification
 */
Notification.fn.show = function() {
  $(this.element).slideDown(200);
  $(this.element).click(this.hide);
}

/**
 * Hide notification
 */
Notification.fn.hide = function() {  
  $(this).animate({opacity: .01}, 200, function() {
    $(this).slideUp(200, function() {
      $(this).remove();
    });
  });
}

/**
 * Log notification
 * 
 * @param  String      Message to be logged
 * @param  String      Type of notification
 */
Notification.fn.log = function(value, type) {
  switch(type) {
    case "information":
      console.info("*** " + value + " ***");
      break;
    case "success":
      console.log(value);
      break;
    case "warning":
      console.warn(value);
      break;
    case "error":
      console.error(value);
      break;
    case "saved":
      console.log(value);
      break;
    default:
      console.log(value);
      break;
  }
}

So what happens is that I try close the notification using the hide function that I believe it gives but I get this error.

Uncaught TypeError: Cannot read property 'defaultView' of undefined

I believe it doesn't know what "this" is but how would I be able to get this to work.

I have this code

$(document).ready(function (){
    var Form_addTurbo = $('form#Form_addTurbo');
    Form_addTurbo.submit(function (e){
        e.preventDefault;
        v = new Notification('Creating Turbo.', 'information', 'Working..', function(){return;});
        $.post('/api/turbos/new.php', {
            action : 'createNew'
        }, function (r){
            v.hide();
            if(r.success){
                new Notification('Turbo Create.', 'saved', '', function(){return;});
            }else if(r.error){

            }else{
                new Notification('Something went wrong.', 'error', '', function(){return;});
            }
        }, 'json');
        return false;
    });
});

Which uses this api

$(document).ready(function(e) {$("body").prepend('<ul id="notifications"></ul>');});

/**
 * Global notification system
 *
 * @param  String      Message to be displayed
 * @param  String      Type of notification
 *
 * @author    Bram Jetten
 * @version    28-03-2011
 */
Notification.fn = Notification.prototype;

function Notification(value, type, tag, onclickfunc) {
  this.log(value, type);
  this.element = $('<li><span class="image '+ type +'"></span>' + value + '</li>');
  if(typeof tag !== "undefined" && tag !== '') {
    $(this.element).append('<span class="tag">' + tag + '</span>');
  }
  if(typeof onclickfunc == 'function'){
      this.element.click(onclickfunc);
  }
  $("#notifications").append(this.element);
  this.show();
}

/**
 * Show notification
 */
Notification.fn.show = function() {
  $(this.element).slideDown(200);
  $(this.element).click(this.hide);
}

/**
 * Hide notification
 */
Notification.fn.hide = function() {  
  $(this).animate({opacity: .01}, 200, function() {
    $(this).slideUp(200, function() {
      $(this).remove();
    });
  });
}

/**
 * Log notification
 * 
 * @param  String      Message to be logged
 * @param  String      Type of notification
 */
Notification.fn.log = function(value, type) {
  switch(type) {
    case "information":
      console.info("*** " + value + " ***");
      break;
    case "success":
      console.log(value);
      break;
    case "warning":
      console.warn(value);
      break;
    case "error":
      console.error(value);
      break;
    case "saved":
      console.log(value);
      break;
    default:
      console.log(value);
      break;
  }
}

So what happens is that I try close the notification using the hide function that I believe it gives but I get this error.

Uncaught TypeError: Cannot read property 'defaultView' of undefined

I believe it doesn't know what "this" is but how would I be able to get this to work.

Share Improve this question edited Dec 8, 2011 at 9:50 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Dec 8, 2011 at 9:45 Bradley WestonBradley Weston 4251 gold badge7 silver badges18 bronze badges 3
  • 1 Have you included jQuery JS file in your HTML <head> tag ? P.S. I didn't downvote ... – Manse Commented Dec 8, 2011 at 9:48
  • 1 Can you create a test case, e.g. at jsfiddle? Your error seems to be originated from the jQuery framework. – Rob W Commented Dec 8, 2011 at 9:51
  • I have just created a jsfiddle URL jsfiddle/aF4TQ/1 hope you can help :) – Bradley Weston Commented Dec 8, 2011 at 11:12
Add a ment  | 

2 Answers 2

Reset to default 0

Made some minor changes to your example. Try it out http://jsfiddle/FLE39/

Had a closer look at your code and made a new jsfiddle. See updated link.

If you are using this in backend node.js then this error es up instead of html-to-image use node-html-to-image.

I have this code

$(document).ready(function (){
    var Form_addTurbo = $('form#Form_addTurbo');
    Form_addTurbo.submit(function (e){
        e.preventDefault;
        v = new Notification('Creating Turbo.', 'information', 'Working..', function(){return;});
        $.post('/api/turbos/new.php', {
            action : 'createNew'
        }, function (r){
            v.hide();
            if(r.success){
                new Notification('Turbo Create.', 'saved', '', function(){return;});
            }else if(r.error){

            }else{
                new Notification('Something went wrong.', 'error', '', function(){return;});
            }
        }, 'json');
        return false;
    });
});

Which uses this api

$(document).ready(function(e) {$("body").prepend('<ul id="notifications"></ul>');});

/**
 * Global notification system
 *
 * @param  String      Message to be displayed
 * @param  String      Type of notification
 *
 * @author    Bram Jetten
 * @version    28-03-2011
 */
Notification.fn = Notification.prototype;

function Notification(value, type, tag, onclickfunc) {
  this.log(value, type);
  this.element = $('<li><span class="image '+ type +'"></span>' + value + '</li>');
  if(typeof tag !== "undefined" && tag !== '') {
    $(this.element).append('<span class="tag">' + tag + '</span>');
  }
  if(typeof onclickfunc == 'function'){
      this.element.click(onclickfunc);
  }
  $("#notifications").append(this.element);
  this.show();
}

/**
 * Show notification
 */
Notification.fn.show = function() {
  $(this.element).slideDown(200);
  $(this.element).click(this.hide);
}

/**
 * Hide notification
 */
Notification.fn.hide = function() {  
  $(this).animate({opacity: .01}, 200, function() {
    $(this).slideUp(200, function() {
      $(this).remove();
    });
  });
}

/**
 * Log notification
 * 
 * @param  String      Message to be logged
 * @param  String      Type of notification
 */
Notification.fn.log = function(value, type) {
  switch(type) {
    case "information":
      console.info("*** " + value + " ***");
      break;
    case "success":
      console.log(value);
      break;
    case "warning":
      console.warn(value);
      break;
    case "error":
      console.error(value);
      break;
    case "saved":
      console.log(value);
      break;
    default:
      console.log(value);
      break;
  }
}

So what happens is that I try close the notification using the hide function that I believe it gives but I get this error.

Uncaught TypeError: Cannot read property 'defaultView' of undefined

I believe it doesn't know what "this" is but how would I be able to get this to work.

I have this code

$(document).ready(function (){
    var Form_addTurbo = $('form#Form_addTurbo');
    Form_addTurbo.submit(function (e){
        e.preventDefault;
        v = new Notification('Creating Turbo.', 'information', 'Working..', function(){return;});
        $.post('/api/turbos/new.php', {
            action : 'createNew'
        }, function (r){
            v.hide();
            if(r.success){
                new Notification('Turbo Create.', 'saved', '', function(){return;});
            }else if(r.error){

            }else{
                new Notification('Something went wrong.', 'error', '', function(){return;});
            }
        }, 'json');
        return false;
    });
});

Which uses this api

$(document).ready(function(e) {$("body").prepend('<ul id="notifications"></ul>');});

/**
 * Global notification system
 *
 * @param  String      Message to be displayed
 * @param  String      Type of notification
 *
 * @author    Bram Jetten
 * @version    28-03-2011
 */
Notification.fn = Notification.prototype;

function Notification(value, type, tag, onclickfunc) {
  this.log(value, type);
  this.element = $('<li><span class="image '+ type +'"></span>' + value + '</li>');
  if(typeof tag !== "undefined" && tag !== '') {
    $(this.element).append('<span class="tag">' + tag + '</span>');
  }
  if(typeof onclickfunc == 'function'){
      this.element.click(onclickfunc);
  }
  $("#notifications").append(this.element);
  this.show();
}

/**
 * Show notification
 */
Notification.fn.show = function() {
  $(this.element).slideDown(200);
  $(this.element).click(this.hide);
}

/**
 * Hide notification
 */
Notification.fn.hide = function() {  
  $(this).animate({opacity: .01}, 200, function() {
    $(this).slideUp(200, function() {
      $(this).remove();
    });
  });
}

/**
 * Log notification
 * 
 * @param  String      Message to be logged
 * @param  String      Type of notification
 */
Notification.fn.log = function(value, type) {
  switch(type) {
    case "information":
      console.info("*** " + value + " ***");
      break;
    case "success":
      console.log(value);
      break;
    case "warning":
      console.warn(value);
      break;
    case "error":
      console.error(value);
      break;
    case "saved":
      console.log(value);
      break;
    default:
      console.log(value);
      break;
  }
}

So what happens is that I try close the notification using the hide function that I believe it gives but I get this error.

Uncaught TypeError: Cannot read property 'defaultView' of undefined

I believe it doesn't know what "this" is but how would I be able to get this to work.

Share Improve this question edited Dec 8, 2011 at 9:50 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Dec 8, 2011 at 9:45 Bradley WestonBradley Weston 4251 gold badge7 silver badges18 bronze badges 3
  • 1 Have you included jQuery JS file in your HTML <head> tag ? P.S. I didn't downvote ... – Manse Commented Dec 8, 2011 at 9:48
  • 1 Can you create a test case, e.g. at jsfiddle? Your error seems to be originated from the jQuery framework. – Rob W Commented Dec 8, 2011 at 9:51
  • I have just created a jsfiddle URL jsfiddle/aF4TQ/1 hope you can help :) – Bradley Weston Commented Dec 8, 2011 at 11:12
Add a ment  | 

2 Answers 2

Reset to default 0

Made some minor changes to your example. Try it out http://jsfiddle/FLE39/

Had a closer look at your code and made a new jsfiddle. See updated link.

If you are using this in backend node.js then this error es up instead of html-to-image use node-html-to-image.

本文标签: javascriptError Cannot read property 39defaultView39 of undefinedusing jQueryStack Overflow