"/"当onclick事件时从URL中删除

"/" Get Removed from the URL When onclick Event

本文关键字:quot URL 删除 onclick 事件      更新时间:2024-01-05

我使用window.history.replaceState()更改网页的当前url,而不刷新页面。以便当任何用户点击该页面上列出的视频并在同一页面中播放同一视频时,我可以将其附加到当前CCD_ 3 YouTube CCD_。

让我们假设样本urls是-

默认视频页面http://www.domain.com/video/用户点击视频URL后,更改为删除最后一个/

http://www.domain.com/video#Se1y2R5QRKU

我希望url保留为/http://www.domain.com/video/#wckLzQDTm6s

这是我正在使用的脚本,但我不确定从url 中删除最后一个/是什么

var regExp = /^.*((youtu.be'/)|(v'/)|('/u'/'w'/)|(embed'/)|(watch'?))'??v?=?([^#'&'?]*).*/;
var match = URL.match(regExp);
if (match && match[7].length == 11) {
    //console.log(match[7]);
    currentURL = document.URL;
    // alert(currentURL.slice(0, currentURL.lastIndexOf('#')));
    console.log(currentURL.slice(0, currentURL.indexOf('#')));
    var sliceURL = currentURL.slice(0, currentURL.indexOf('#'));
    var newURL = sliceURL + '#' + match[7];
    window.history.replaceState(null, "Video Gallery", newURL);
    return match[7];
} else {
    //alert("Could not extract video ID.");
}

因为当前URL最初不包含#,indexOf将返回-1,结果是最后一个字符(斜杠)将被删除。在对其进行切片之前,您应该明确检查哈希是否在当前URL中:

var hashIndex = currentURL.indexOf('#');
var sliceURL = (hashIndex != -1) ? currentURL.slice(0, hashIndex) : currentURL;