Javascript-форум (https://javascript.ru/forum/)
-   Мобильный JavaScript (https://javascript.ru/forum/mobile/)
-   -   Как запретить переход по ссылкам? (https://javascript.ru/forum/mobile/51330-kak-zapretit-perekhod-po-ssylkam.html)

Al Jo 02.11.2014 23:05

Как запретить переход по ссылкам?
 
Уважаемые гуру.

Возможно ли, с помощью скрипта, запретить переход по ссылкам в мобильном браузере (iOS)?.

Есть такой скрипт:

SingleTapDetector = function(element, handler) {
this.element = element;
this.handler = handler;

element.addEventListener('touchstart', this, false);
};

SingleTapDetector.prototype.handleEvent = function(event) {
switch (event.type) {
case 'touchstart': this.onTouchStart(event); break;
case 'touchmove': this.onTouchMove(event); break;
case 'touchend': this.onTouchEnd(event); break;
}
};

SingleTapDetector.prototype.onTouchStart = function(event) {
this.element.addEventListener('touchend', this, false);
document.body.addEventListener('touchmove', this, false);

this.startX = this.currentX = event.touches[0].clientX;
this.startY = this.currentY = event.touches[0].clientY;
this.startTime = new Date().getTime();
};

SingleTapDetector.prototype.onTouchMove = function(event) {
this.currentX = event.touches[0].clientX;
this.currentY = event.touches[0].clientY;
};

SingleTapDetector.prototype.onTouchEnd = function(event) {
var that = this;

// Has there been one or more taps in this sequence already?
if (this.tapTimer) {
// Reset the timer to catch any additional taps in this sequence
clearTimeout(this.tapTimer);
this.tapTimer = setTimeout(function() {
that.tapTimer = null;
}, 300);
} else {
// Make sure the user didn't move too much
if (Math.abs(this.currentX - this.startX) < 4 &&
Math.abs(this.currentY - this.startY) < 4) {
// Make sure this isn't a long press
if (new Date().getTime() - this.startTime <= 300) {
// Make sure this tap wasn't part of a selection event
if (window.getSelection() + '' == '') {
// Make sure this tap is in fact a single tap
this.tapTimer = setTimeout(function() {
that.tapTimer = null;

// This is a single tap
that.handler(event);
}, 300);
}
}
}
}
};

С помощью скрипта я могу получить новый адрес документа, а хотелось бы получать именно url объекта тапа.

new SingleTapDetector(document.body, function(event) {
document.location = "internal://" + document.URL;
});

В ДОПОЛНЕНИЕ, я хочу отключить, собственно, переход по ссылке и обрабатывать переход уже по своему сценарию в iOS.

new SingleTapDetector(document.body, function(event) {
document.location = "internal://" + document.URL;
return false; <--- не работает!
});

Укажите направление. Спасибо

Safort 03.11.2014 01:41

А так?
new SingleTapDetector(document.body, function(event) {
    event.preventDefault();
    document.location = "internal://" + document.URL;
});

Al Jo 03.11.2014 02:33

Не работает :(

я могу заменить все ссылки на мусор, например вот так:

var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
links[i].setAttribute("href", "blabla");
}

Тогда переход по ссылке не происходит... Но как сохранить их (ссылки) до замены в массив и потом сравнить кликнутый элемент с элементом из этого массива? :help:

Vlasenko Fedor 03.11.2014 04:04

document.links содержит все ссылки. Не у всех ссылок есть атрибут href


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