Javascript java脚本中`Response.Redirect("~/abc.aspx")的等效代码是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6649461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the equivalent code of `Response.Redirect("~/abc.aspx") in java script?
提问by French Boy
What is the equivalent code of Response.Redirect(~/Account/Login.aspx");
in javascript?
Response.Redirect(~/Account/Login.aspx");
javascript中的等价代码是什么?
I tried : window.location="~/Account/Login.aspx"
but the ~
is not accepted in javascript. so, what is the alternative code?
我试过:window.location="~/Account/Login.aspx"
但~
在javascript中不被接受。那么,替代代码是什么?
Note: the javascript script is made in the server side in the Page_Load
method by using ClientScript.RegisterClientScriptBlock
.
注意:javascript 脚本是在服务器端Page_Load
通过使用ClientScript.RegisterClientScriptBlock
.
回答by Ales Ruzicka
use
用
window.location='<%= ResolveUrl("~/Account/Login.aspx") %>'
EDIT: if it is created in codebehind, then use
编辑:如果它是在代码隐藏中创建的,则使用
string.Format("window.location='{0}';", ResolveUrl("~/Account/Login.aspx"))
回答by Sjoerd
Try this:
尝试这个:
window.location='<%= ResolveUrl("~/Account/Login.aspx") %>';
The ~ is replaced by the application URL in .NET, but this is not done in Javascript.
~ 在 .NET 中被应用程序 URL 替换,但这不是在 Javascript 中完成的。
回答by Matthew Abbott
Try:
尝试:
Page.RegisterClientScriptBlock(typeof(_Default), "Redirect", "document.location.href = '" + ResolveUrl("~/Account/Login.aspx") + "';", true);
I would ask as to why you are doing a client-side redirect from the server-side though? Would it not be more appropriate to do a Response.Redirect
instead?
我会问你为什么要从服务器端进行客户端重定向?改为 aResponse.Redirect
不是更合适吗?