首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何重定向到另一个页面并强制刷新该页面,以便运行Javascript

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

Stack Overflow用户
提问于 2019-06-10 23:58:16
回答 1查看 256关注 0票数 0

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

  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>

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56529734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档