js 执行跨域刷新页面

主要代码:

注意这段代码 是子页面中添加的也就是弹出的那个页面刷新父页面

   <script type="text/javascript">

        function shuaxin() {
            try {
                window.parent.opener.location.reload();
                window.parent.close();
            } catch (e) {
                window.parent.opener.location = "http://localhost:32859/test.aspx";
                window.parent.opener = null;
                window.parent.close();
            }
        }

    </script>

测试demo

首先新建一个web1,再新建一个test.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApp02.test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>

    <script type="text/javascript">

        function OpenPage() {

            window.open('http://localhost:32858/testweb2.aspx', 'newwindow', 'height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no')

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <asp:Label ID="lblTest" runat="server" Text="还没开始测试"></asp:Label>
            <input id="Button1" type="button" value="button" onclick="OpenPage()" />

        </div>
    </form>
</body>
</html>

test.aspx 后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApp02
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                lblTest.Text = "页面执行时间:" + DateTime.Now.ToLocalTime();
            }
            Load();
        }

        public void Load()
        {
            lblTest.Text = "页面执行时间:" + DateTime.Now.ToLocalTime();
        }


    }
}

再次新建一个web2,再新建一个testweb2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testweb1.aspx.cs" Inherits="WebApp01.testweb1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>

    <script type="text/javascript">

        function shuaxin() {
            try {
                window.parent.opener.location.reload();
                window.parent.close();
            } catch (e) {
                window.parent.opener.location = "http://localhost:32859/test.aspx";
                window.parent.opener = null;
                window.parent.close();
            }
        }

    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input id="Text1" type="button" value="关闭当前页面" onclick="shuaxin()" />
        </div>
    </form>
</body>
</html>

 参考文章:http://www.xuebuyuan.com/838707.html

原文地址:https://www.cnblogs.com/foreverfendou/p/7753706.html