admin管理员组

文章数量:1405603

I have a set of code

setTimeout(function() {
   window.location.reload(true);
   if ($scope.attribute.parentAttribute.id) {
       angular.element(document.getElementById($scope.attribute.parentAttribute.id)).click();
   }
}, 1000);

I am using a timeout, I need to execute that click function after window.location.reload(true); reload cause by this line. Is it possible?

I have a set of code

setTimeout(function() {
   window.location.reload(true);
   if ($scope.attribute.parentAttribute.id) {
       angular.element(document.getElementById($scope.attribute.parentAttribute.id)).click();
   }
}, 1000);

I am using a timeout, I need to execute that click function after window.location.reload(true); reload cause by this line. Is it possible?

Share Improve this question edited Mar 16, 2017 at 10:58 T J 43.2k13 gold badges86 silver badges142 bronze badges asked Feb 18, 2016 at 5:32 vkvvkv 1,0205 gold badges23 silver badges48 bronze badges 4
  • nope , you are reloading the page before you trigger click – madalinivascu Commented Feb 18, 2016 at 5:34
  • 2 Yes, set a flag in local storage, then reload, then test that flag from an onload or DOM ready handler and trigger the click from there. – nnnnnn Commented Feb 18, 2016 at 5:36
  • You would need to do it on the load event. – epascarello Commented Feb 18, 2016 at 5:36
  • any one can demonstrate please? – vkv Commented Feb 18, 2016 at 5:40
Add a ment  | 

4 Answers 4

Reset to default 1

Define your handler for the onload event here. This will trigger after a page load (reload is no different):

object.onload=function(){myScript}; //in this case object = window

Source: http://www.w3schools./jsref/event_onload.asp

You can call the onload method from your starting body tag:

<body onload="myFunction()">

Well, once you have called window.location.reload(true); then 1. your page will refresh/reload. 2. all the javascript will reload on the page. 3. all the triggers or callbacks will be destroyed/cancelled.

So in short there is no way to call a function after a reload call is made.

All you can do is call a function at page load time as shown below.

$(function(){
  // this will be executed first when page loads.
});

But there is one glitch with this, this will execute the function whenever the page is loaded.

$window.onload = function() {
  /*do your thing*/
};

You will need to inject $window into your controller or run, etc

本文标签: javascriptIs that possible to execute a function just after reloadStack Overflow