0

I have a web page where I am using javascript to redirect to another page. The second page displays correctly but I can't get javascript to run when the second page is loaded. Here are 3 ways I have tried to get my javascript function to load on the second page:

  1. Put the function as onload parameter for html body
  2. Tried calling it as inline javascript inside both the heading and body of the page
  3. Used jquery $(document).ready to try and call the function when page loads

I simplified the function to be a simple alert to make sure the problem wasn't with the function code and also set break point on function to make sure it wasn't being called. The code won't fire when the page is loaded but if I do a refresh on the browser it will work.

The page I am trying to redirect to is generated by a 3rd party application we have so I am limited on how much I can modify it but I can make basic HTML/javascript changes to the page. The second page is also built with Angular.

Any ideas on how I can get this second page to always run my javascript code when I redirect to it? Below is code from second page with the myTest() the function I am trying to run. If I hit refresh on browser when this page loads, I get the myTest alert 3 times.

<!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>
Chris
  • 379
  • 1
  • 6
  • 19
  • Please share the code for your page, it's just a guessing game without it. – frobinsonj Jun 10 '19 at 16:02
  • Sorry, I just added code to my original post. Let me know if you need me to provide anything else. – Chris Jun 10 '19 at 16:39
  • You can not run anything on another page for security reasons -- imagine sites could redirect you to google and run something there. You can try iframe/new window -- with right security settings (or same domain) you can run something. – Petr Averyanov Jun 10 '19 at 17:30
  • I'm not trying to start the 2nd page and tell it to run something when it loads. I realize that would be a huge security risk. The page that does the redirect and the page it is going to both belong to us and are on same web server. Issue is when second page loads it doesn't execute the code above unless I hit refresh button. – Chris Jun 10 '19 at 18:47

1 Answers1

0

What browser are you using?

I've seen something similar happen in Firefox, meanwhile the same is working in other browser.

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

altair2195
  • 23
  • 4