如何重定向到另一个页面并在该页面上强制刷新,以便运行Javascript

时间:2019-06-10 15:58:15

标签: javascript angularjs refresh

我有一个网页,我在其中使用javascript重定向到另一个页面。第二页显示正确,但是加载第二页时无法运行javascript。这是我尝试将javascript函数加载到第二页上的3种方法:

  1. 将函数作为html正文的onload参数
  2. 试图在页面标题和正文中将其称为内联javascript
  3. 使用jquery $(document).ready尝试在页面加载时调用该函数

我将函数简化为简单警报,以确保问题不在于函数代码,并且在函数上设置断点以确保未被调用。加载页面后,代码不会触发,但是如果我在浏览器中刷新,它将可以正常工作。

我要重定向到的页面是由我们拥有的第三方应用程序生成的,因此我只能对其进行修改,但是我可以对该页面进行基本的HTML / javascript更改。第二页也是使用Angular构建的。

关于如何使第二页重定向到我的JavaScript代码时始终运行我的JavaScript代码的任何想法?下面是第二页的代码,其中包含我要运行的功能myTest()。如果在加载此页面时在浏览器上单击“刷新”,则会收到3次myTest警报。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <title ng-controller="TitleCtrl" ng-bind="title"></title>

    <!-- third party stylesheets -->
    <link rel="stylesheet" type="text/css" href="third-party/bootstrap-3.3.5/css/bootstrap.min.css" />
    <link rel="styleSheet" type="text/css" href="third-party/angular-ui-grid-3.1.1/ui-grid.min.css" />
    <link rel="stylesheet" type="text/css" href="third-party/font-awesome-4.7.0/css/font-awesome.min.css" />

    <script type="text/javascript" src="angular-1.5.0/angular.min.js"></script>
    
	//editing out list of about 50 javascript files to make code example shorter
	
<script type="text/javascript">
  function myTest() {
    alert("My Test");
  }
</script>	
</head>
    <body class="app-body" onload="myTest();">
        <ng-include src="'components/property-pages/field-scripts.html'"></ng-include>
        <ng-include src="'components/form-editor/form-editor-field-scripts.html'"></ng-include>
        <ng-include src="'components/form-editor/field-properties-dialog-definition-scripts.html'"></ng-include>
        <ng-include src="'components/form-editor/field-properties-scripts.html'"></ng-include>
        <ng-include src="'components/lib/custom-handler/custom-handler-tooltip-script.html'"></ng-include>

        <!-- alert notification -->
        <div ng-include="'components/alerts/alert.html'"
             ng-controller="AlertController as vm">
        </div>

        <!-- navigation bar -->
        <navbar></navbar>

        <!-- main container -->
        <div id="main-container"
             ng-controller="MainContainerController as vm"
             ng-style="{ top: vm.getTop(), height: vm.getHeight() }">
            <ng-view></ng-view>
        </div>

        <!-- loading status panel -->
        <div ng-include="'components/loading/loading.html'"
             ng-controller="LoadingController as vm">
        </div>

        <!-- pre-bootstrap loading status panel -->
        <div class="startup-loading">
            <span class="fa fa-spinner fa-pulse fa-3x fa-fw"></span>
        </div>

        <!-- pre-bootstrap error status panel -->
        <div class="startup-error">
            <span class="fa fa-exclamation-circle fa-3x fa-fw"></span>
            <div id="startup-error"></div>
        </div>
		
		<script type="text/javascript">
		  myTest();
		  
		  $( document ).ready(function() {
			myTest();
		  });
		</script>		
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

您使用的是哪种浏览器?

我已经在Firefox中看到了类似的情况,同时在其他浏览器中也是如此。

$(document).ready() not firing after redirect with window.location