这两条线之间有区别吗?
var url = "http://www.google.com/";
window.location = url;
window.location.replace(url);
Run Code Online (Sandbox Code Playgroud) 我有一些像这样的代码,
<form id="abc">
<input type="text" id="txt" />
</form>
Run Code Online (Sandbox Code Playgroud)
现在我想像这样重定向,
var temp = $("#txt").val();
url = "http://example.com/" + temp;
window.location.replace(url);
// or window.location(url);
Run Code Online (Sandbox Code Playgroud)
无论如何在jQuery中解决这个问题?它仍然让我拥有url = http://example.com.
有什么区别
window.location.href="http://example.com";window.location.replace("http://example.com");window.location.assign("http://example.com");我在许多论坛中读到window.location.assign()只是替换当前的会话历史记录,因此浏览器的后退按钮将不起作用.但是,我无法重现这一点.
function fnSetVariable() {
//window.location.href = "http://example.com";
window.location.replace("http://example.com");
//window.location.assign("http://example.com");
}
<a onmouseover="fnSetVariable();"
href="PageCachingByParam.aspx?id=12" >
CLICK
</a>
Run Code Online (Sandbox Code Playgroud) 由于我正在使用react-router来处理反应应用程序中的路由,所以我很好奇是否有办法重定向到外部资源.
说某人点击:
example.com/privacy-policy
我希望它重定向到:
example.zendesk.com/hc/en-us/articles/123456789-Privacy-Policies
我正在寻找零帮助,以避免在我的index.html加载时用简单的JS编写它,例如:
if ( window.location.path === "privacy-policy" ){
window.location = "example.zendesk.com/hc/en-us/articles/123456789-Privacy-Policies"
}
Run Code Online (Sandbox Code Playgroud) 在JavaScript内存消耗之间是否有任何特定的优势/劣势使用location.href = url而不是location.assign(url)?
我想我想知道是否需要更多的内存来访问该方法而不是设置属性.
我使用以下TypeScript代码收到错误:
///<reference path='../../../Shared/typescript/jquery.d.ts' />
///<reference path='../../../Shared/typescript/jqueryStatic.d.ts' />
function accessControls(action: Action) {
$('#logoutLink')
.click(function () {
var $link = $(this);
window.location = $link.attr('data-href');
});
}
Run Code Online (Sandbox Code Playgroud)
我收到以下带下划线的红色错误:
$link.attr('data-href');
Run Code Online (Sandbox Code Playgroud)
消息说:
Cannot convert 'string' to 'Location': Type 'String' is missing property 'reload' from type 'Location'
Run Code Online (Sandbox Code Playgroud)
有谁知道这意味着什么?
我很困惑,之间的差异window.location和location.href.两者似乎都以同样的方式行事.
有什么不同?
什么window.origin啊 它似乎没有在通常的地方记录下来。
看起来可能非常类似于window.location.origin-例如,在此处的Stack Overflow中,两者都返回
Run Code Online (Sandbox Code Playgroud)https://stackoverflow.com
但是在中iframe,它们是不同的:
https://stackoverflow.com
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)https://stacksnippets.net null
嵌入代码段是一个内部iframe 无 allow-same-origin。例如,如果您更改iframe,则您编辑Stack Overflow的HTML并手动添加属性:
<iframe name="313b857b-943a-7ffd-4663-3d9060cf4cb6" sandbox="allow-same-origin allow-forms allow-modals allow-scripts" class="snippet-box-edit" frameborder="0" style="">
^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
然后运行该代码段,您将获得:
Run Code Online (Sandbox Code Playgroud)https://stacksnippets.net https://stacksnippets.net
带有<iframe>s的其他网站也表现出相同的行为。
Google在此主题上似乎没有任何权威链接。搜索确切的短语 + Javascript会得到许多与,iframe和有关的结果postMessage,但没有确切说明window.origin 实际是什么。
postMessage从子级iframe进行调用似乎会导致父窗口接收到一条消息,该消息具有与子级框架的origin属性匹配的消息window.origin-不带allow-same-origin,即为null,否则看起来window.location.origin与子级相同。
以上是我认为从猜测和检查中得出的结论,但我还不确定。非常感谢您的确认/解释,最好是带有权威来源的链接。
我发现自己总是写作:
console.log(window.location.href);
Run Code Online (Sandbox Code Playgroud)
甚至没有考虑过它.关于SO的大多数答案也是这样写的.我有什么理由不能写:
location.href
Run Code Online (Sandbox Code Playgroud)
既然location是窗口级别的对象?这有什么跨浏览器兼容性问题吗?
澄清:我知道有document.location- 这不是这个问题的内容.这location与使用window.location跨浏览器仅使用vs 有任何区别.
当我在浏览器中测试这些代码时,它们似乎是相同的.有什么区别吗?
window.location = "http://stackoverflow.com";
Run Code Online (Sandbox Code Playgroud)
window.location.href = "http://stackoverflow.com";
Run Code Online (Sandbox Code Playgroud) javascript ×10
jquery ×2
location ×2
redirect ×2
cross-domain ×1
dom ×1
html ×1
iframe ×1
react-router ×1
reactjs ×1
typescript ×1
url ×1
window ×1