Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更新窗口哈希(在url处)_Javascript_Hash_Window_Photo Gallery - Fatal编程技术网

Javascript 更新窗口哈希(在url处)

Javascript 更新窗口哈希(在url处),javascript,hash,window,photo-gallery,Javascript,Hash,Window,Photo Gallery,我有一个图像库的js代码: (this.settings.update_window_hash) { var thumb_link = this.images[this.current_index].thumb_link; if (thumb_link.attr("id")) { window.location.hash = "#image-"+ thumb_link.attr("id"); //#url } else {

我有一个图像库的js代码:

(this.settings.update_window_hash) {
        var thumb_link = this.images[this.current_index].thumb_link;
        if (thumb_link.attr("id")) {
          window.location.hash = "#image-"+ thumb_link.attr("id"); //#url
        } else {
          window.location.hash = "#image-"+ this.current_index;
        };
};
因此,正如您可能假设的那样,这会将$image-(int)附加到url。所以如果我有一个 具有多个图像的库如果选择了第三个图像,url将如下所示: mysite.com/gallery.html#image-3

一切都好。但是我真的不喜欢这个被附加到url的末尾。也有 如果我完全删除脚本的这一部分,有什么问题吗?所以不管有多少人 当前选定的图像url将如下所示: mysite.com/gallery.html

我已经测试过了,效果很好。但我对javascript不是很有经验,我希望 以确保我没有犯错误。所以 完全删除此脚本可以吗?它会导致任何问题吗?

非常感谢。

只需使用:

location.replace(location.href + "#myhash");

location.replace方法覆盖浏览器历史记录中的当前步骤。有关此操作的示例,请参见URL末尾的哈希是可选的,不是必需的。因此,是的,如果需要,您可以删除该脚本(我不确定通过删除它来解决什么问题)。一般来说,如果你告诉我们你想解决什么问题,而不是你想使用什么解决方案,你会得到更有用的答案


当您希望页面的URL将查看器指向该页面上的某个子元素时,可以使用哈希。如果删除它们,页面仍能正常工作,但页面的URL不会反映正在显示的图像。因此,如果查看器保存该URL并返回到该URL或链接到该URL或保留对该URL引用的任何内容,它将转到页面的通用版本,而不是显示特定图像的页面。这是否正确完全取决于您和您的页面如何工作。

OctorType后面的内容通常表示网页中的“名称”或“id”。你可以有一个锚标签(
了解标准用法的更多说明。

只要你不在代码中读取URL中的哈希值,你应该很好。你好。这回答了我的问题。我真的不想在URL中看到它们,因为它们在我的情况下毫无用处。js是图库/图像滑块的一部分,URL只显示当前选定的图像,所以我不是我们在网页或其他地方(它可以帮助导航到页面的所需部分)下载。谢谢你的回答。是的,我知道。但正如我所说,因为我是js的新手,所以我只是想确保我没有做错任何事情。