S
S
Selsh2014-06-15 01:13:47
JavaScript
Selsh, 2014-06-15 01:13:47

Why does IE8 throw a "document defaultview is null or not an object" error?

IE 8 throws a "document defaultview is null or not an object" error and the code doesn't work.
js code.

var ul;
var liItems;
var imageNumber;
var imageWidth;
var prev, next;
var currentPostion = 0;
var currentImage = 0;


function init(){
  ul = document.getElementById('image_slider');
  liItems = ul.children;
  imageNumber = liItems.length;
  imageWidth = liItems[0].children[0].clientWidth;
  ul.style.width = parseInt(imageWidth * imageNumber) + 'px';
  prev = document.getElementById("prev");
  next = document.getElementById("next");
  generatePager(imageNumber);
  prev.onclick = function(){ onClickPrev();};
  next.onclick = function(){ onClickNext();};
}

function animate(opts){
  var start = new Date;
  var id = setInterval(function(){
    var timePassed = new Date - start;
    var progress = timePassed / opts.duration;
    if (progress > 1){
      progress = 1;
    }
    var delta = opts.delta(progress);
    opts.step(delta);
    if (progress == 1){
      clearInterval(id);
      opts.callback();
    }
  }, opts.delay || 17);
}

function slideTo(imageToGo){
  var direction;
  var numOfImageToGo = Math.abs(imageToGo - currentImage);

  direction = currentImage > imageToGo ? 1 : -1;
  currentPostion = -1 * currentImage * imageWidth;
  var opts = {
    duration:1000,
    delta:function(p){return p;},
    step:function(delta){
      ul.style.left = parseInt(currentPostion + direction * delta * imageWidth * numOfImageToGo) + 'px';
    },
    callback:function(){currentImage = imageToGo;}	
  };
  animate(opts);
}

function onClickPrev(){
  if (currentImage == 0){
    slideTo(imageNumber - 1);
  } 		
  else{
    slideTo(currentImage - 1);
  }		
}

function onClickNext(){
  if (currentImage == imageNumber - 1){
    slideTo(0);
  }		
  else{
    slideTo(currentImage + 1);
  }		
}

function generatePager(imageNumber){	
  var pageNumber;
  var pagerDiv = document.getElementById('pager');pager
  for (i = 0; i < imageNumber; i++){
    var li = document.createElement('li');
    pageNumber = document.createTextNode(parseInt(i + 1));
    li.appendChild(pageNumber);
    pagerDiv.appendChild(li);
    li.onclick = function(i){
      return function(){
        slideTo(i);
      }
    }(i);
  }	
  var computedStyle = document.defaultView.getComputedStyle(li, null);
  var liWidth = parseInt(li.offsetWidth);
  var liMargin = parseInt(computedStyle.margin.replace('px',""));
  pagerDiv.style.width = '300px';
}
window.onload = init;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Lobanov, 2014-06-15
@Selsh

help.dottoro.com/ljvftsxk.php
Try instead:
Write:

var doc_d = document.defaultView ? document.defaultView : document.parentWindow;
var computedStyle = doc_d.getComputedStyle(li, null);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question