Javascript-форум (https://javascript.ru/forum/)
-   Events/DOM/Window (https://javascript.ru/forum/events/)
-   -   Ошибка: Uncaught TypeError: Cannot set property 'innerHTML' of null (https://javascript.ru/forum/events/41440-oshibka-uncaught-typeerror-cannot-set-property-%27innerhtml%27-null.html)

dolte 13.09.2013 17:52

Ошибка: Uncaught TypeError: Cannot set property 'innerHTML' of null
 
Проверяем на наличие хэша в строке браузера
если http://mysite.ru#1 то ничего не показываем
если http://mysite.ru#2 или http://mysite.ru#3 или http://mysite.ru#4

то выводим alert и данные из файла, ошибка показывается, но файл не вставляется.

в консоли:
Uncaught TypeError: Cannot set property 'innerHTML' of null

<script>
var hah = window.location.hash;


var requestik = new XMLHttpRequest();requestik.open('GET', '/complete/moonbeam-suhov/page1.html');
requestik.send(null)
var html = document.documentElement;
var elementik = document.querySelector('.showphoto1');

if( hah >= "#2"){
	alert('хеш');
	elementik.innerHTML = requestik.responseText;
}     

</script>
<div class="showphoto1"></div>

devote 13.09.2013 17:53

оберни код в window.onload = function() { ... }

dolte 16.09.2013 10:14

Код обернул, с алертом файл подключается, убираю алерт, файл не подключается.
Причем в опере12 работает, а в хроме только алерт.

devote 16.09.2013 10:28

window.onload = function() {
    var requestik = new XMLHttpRequest();
    requestik.open('GET', '/complete/moonbeam-suhov/page1.html');
    requestik.onreadystatechange = function() {
        if (this.readyState === 4 && (this.status >= 200 && this.status < 300 || this.status === 304)) {
            var hah = window.location.hash;
            var html = document.documentElement;
            var elementik = document.querySelector('.showphoto1');
            if (hah >= "#2") {
                alert('хеш');
                elementik.innerHTML = requestik.responseText;
            }
        }
    }
    requestik.send(null);
}

dolte 16.09.2013 10:54

А как объединить это с fancybox ?
вот код вызова:

var thisHash = window.location.hash;
$(document).ready(function() {
if(window.location.hash) {
$(thisHash).fancybox({
})
.trigger('click');
}	
$('.fancybox').attr('rel', 'gallery').fancybox({
prevEffect : 'fade',
nextEffect : 'fade',
closeBtn  : true,
arrows    : true,
nextClick : true,
beforeClose: function() {
window.location.hash = '';
},
afterLoad: function() {
$.getScript("/share.js");
var el, id = $(this.element).data('title-id');
window.lnkimg = this.href;
if(id){
el = $('#'+id);
if(el.length){
this.title = el.html();
}
}
window.location.hash = this.index;
},});});


т.е кодга открываем фото, у нас к адресу добавляется хеш, допустим
http://mysite.com#3
и по этой ссылке срабатывает
.trigger('click');
открывается выбранное фото.


Часовой пояс GMT +3, время: 13:26.