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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (53)

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

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

我将该函数简化为一个简单的警报,以确保问题不在于函数代码,并在函数上设置断点以确保它没有被调用。加载页面时代码不会触发,但如果我在浏览器上刷新它将起作用。

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

当我重定向到它时,有关如何让第二页始终运行我的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>
提问于

扫码关注云+社区

领取腾讯云代金券