我有这样的URL:http://example.com#something如何删除#something,而不会导致页面刷新?
我尝试了以下解决方案:
window.location.hash = '';
Run Code Online (Sandbox Code Playgroud)
但是,这不会#从URL中删除哈希符号.
有没有办法检测HTTP或HTTPS,然后强制使用HTTPS与JavaScript?
我有一些代码用于检测HTTP或HTTPS,但我不能强制它使用https:.
我正在使用window.location.protocol属性来设置站点的任何内容,https:然后刷新页面以希望重新加载加载到浏览器中的新https'ed URL.
if (window.location.protocol != "https:") {
window.location.protocol = "https:";
window.location.reload();
}
Run Code Online (Sandbox Code Playgroud) 他们俩都应该引用同一个对象吗?
如何强制Web浏览器通过JavaScript对页面进行硬刷新?
硬刷新意味着获取页面的新副本并刷新所有外部资源(图像,JavaScript,CSS等).
当重定向到新页面时,window.location.assign()和之间有什么区别window.location.replace()?
在网络上,我看到大量的JavaScript程序员写作window.location而不仅仅是location.我很好奇是否有人可以提供解释原因.window是全局对象,因此没有必要包括 - 不是吗?我的意思是,你不要看的人写的window.Math.floor还是new window.Date(),所以我很好奇,为什么它会与指定location.
我理解这location被认为是你所在窗口的"属性",我认为这是有道理的.但即便如此,我也没有理由指定全局对象; 首先不可能覆盖location,不是没有重定向页面.
那么,这只是一个长期使用的怪癖,它与我们编写JavaScript的方式相结合,还是有一些切实的理由以这种方式做事?我查了一下谷歌,但是唉,我什么也没想到......
我有以下代码:
$('ul.questions li a').click(function(event) {
$('.tab').hide();
$($(this).attr('href')).fadeIn('slow');
event.preventDefault();
window.location.hash = $(this).attr('href');
});
Run Code Online (Sandbox Code Playgroud)
这简单地根据您点击的时间淡化div,但我希望您点击时更改页面URL哈希标记,以便人们可以复制和添加书签.目前,当哈希标签发生变化时,这会有效地重新加载页面.
是否可以更改哈希标记而不重新加载页面以防止跳跃效应?
这是我的代码:
<a href="http://www.google.com" onClick="window.location.href='http://www.yahoo.com';return false;" target="_blank">test</a>
Run Code Online (Sandbox Code Playgroud)
当你点击它,它会带你到雅虎,但它没有打开一个新窗口?
我学习了"window.location.hash" new并在我的jquery代码中尝试而不是"window.location.href",并且它们都给出了相同的结果.
代码在这里:
window.location.href = ($(e.currentTarget).attr("href"));
window.location.hash = ($(e.currentTarget).attr("href"));
Run Code Online (Sandbox Code Playgroud)
他们之间有什么区别?
我目前正在设置window.location.pathname属性以将用户重定向到相对URL.新URL有参数,因此JavaScript行如下所示:
window.location.pathname = window.location.pathname.substring( 0, window.location.pathname.lastIndexOf( '/' ) + 1 ) + 'myPage.xhtml?u=' + selected_user.Username;
Run Code Online (Sandbox Code Playgroud)
这在Firefox中很成功,但Chrome使用'%3F'对问号进行编码,然后请求失败.
我不确定我是否正确使用window.location.我是否需要使用window.location的属性,例如pathname或href?我发现,只要我设置一个属性的位置被重新加载,因此,例如,搜索和路径属性不能单独设置.可以直接设置window.location吗?我只需要用参数设置相对URL.
window.location ×10
javascript ×8
hash ×2
jquery ×2
reload ×2
href ×1
html ×1
https ×1
hyperlink ×1
location ×1
page-refresh ×1