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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:37:28  来源:igfitidea点击:

What is the equivalent code of `Response.Redirect("~/abc.aspx") in java script?

javascriptasp.net

提问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_Loadmethod 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.Redirectinstead?

我会问你为什么要从服务器端进行客户端重定向?改为 aResponse.Redirect不是更合适吗?