Javascript:访问地址栏并重定向到新网址?

时间:2015-05-31 22:12:27

标签: javascript

我已经获得了一个旧的Javascript程序,该程序将一些状态信息保存在URL中,经过"#"这样状态信息就不会传递给HTTP服务器了。

        if (theDialog.ShowDialog() == DialogResult.OK)
        {
            string filename = theDialog.FileName;
            string s = File.ReadAllText(filename);

            this.richTextBox1.SelectionStart = 0;  // or wherever you want to insert..
            this.richTextBox1.SelectionLength = 0;
            this.richTextBox1.SelectionFont = new System.Drawing.Font("Maiandra GD", 30);
            this.richTextBox1.SelectdText = s;
        }

我需要重写程序,以便在服务器上处理参数。此外,我想将旧网址(例如,保存在电子邮件中)重定向到新服务。

我的问题:

如何打开旧表单的URL,让Javascript重写URL并重定向到新重写的URL?出于讨论目的,翻译"#"到" @"会是完美的。

即点击此链接:

http://example.com/oldprogram/#/some-parms

导致加载此页面:

http://example.com/oldprogram/#/some-parms

1 个答案:

答案 0 :(得分:2)

您可以使用:

window.location = ("" + window.location).replace("#", "@");

这会根据您的要求重定向到生成的网址。 显然,您必须将此脚本放入旧页面。

如果这不是一个选项,你就无法用JS强制重定向。 还有其他选项,如URL重写和通过Web服务器配置重定向。

对于Apache,作为示例,您将使用mod_rewrite