0

I am trying to figure out how to use a single click to do a refresh immediately before the function showX() runs. I have tried putting a semi-colon such as location.reload();showX() but then the code doesn't run. This seamingly easy function has become very complicated. I have searched everywhere but no luck so far.

<div align="center"><button onclick="location.reload();"> <strong>Refresh</strong> Before Select </button>

<div align="center"><button id="b1" onclick="showX(2)"> 178-0416-00 </button>
JohnB
  • 133
  • 1
  • 2
  • 13
  • Why do you need the page to reload before you execute the function? Depending on the need, you could simply add the call to refresh to `showX()`. – Vishal Kotcherlakota Apr 24 '14 at 17:02
  • Look at solution using jQuery there http://stackoverflow.com/a/1756598/588973 – Deele Apr 24 '14 at 17:03
  • 1
    Think carefully, what happens when you refresh a page. It is reloaded, right? Everything you had on the previous page is gone (including scripts and HTML, fired events, all), and the page is parsed and rendered again, from scratch... – Teemu Apr 24 '14 at 17:09
  • 1
    You have a _solution_ in mind, "run the showX proc _after_ the page reloads", but what is the actual _problem_ you're trying to solve by doing this? There may be A Better Way. – Stephen P Apr 24 '14 at 17:45

4 Answers4

1

When you rise reload method the page will lose all references and doesn't execute ShowX;

You will need to use something like a coockie or query string to mantein the page´s state and in a method like in JQuery

$(document).ready(function () {});

Can you tell what do you want to do? With this we can help suggesting something to solve your problem

Samuel
  • 1,149
  • 8
  • 25
  • The first time a button is pressed say "showX(1)" it configures a document. When pressing another "showX(2)" it opens the second document. Unfortunately, the two documents combine so a refresh is needed. Maybe some kind of unload will work instead of refresh as well. – JohnB Apr 24 '14 at 21:46
  • What do you think to call an Ajax function instead reload the page. This way you will have all control and will be able to call "showX" at any time. – Samuel Apr 25 '14 at 09:30
0

You can add a GET parameter to the query so that, if that parameter is present, the function showX will be executed as soon as the document is fully loaded.

It's not an elegant solution but this is because your request is very unusual itself.

dogiordano
  • 691
  • 6
  • 13
0

TO make a function run after a refresh is done could be:

 window.onload =  function showX()
                     {
                  //Rest of code
                     }

window.onload is a nice way to run a function when the page has been loaded without a button

Brendan
  • 1,399
  • 1
  • 12
  • 18
0

I was able to answer my own question by going a little different route. I decided to use a button click counter as follows. I have 8 buttons so I did this for each button. If I click any of the buttons the first time it loads the code. If I press the same or any other button it reloads the page. It takes two clicks but it still gets rid of my other refresh button where people may for get to click.

Thank you everyone for your input. I would have gone with the cookies route but I really didn't want everyone having to enable cookies which is disabled by IT.

var count = 1;
document.getElementById('b1').onclick = function() {
   location.reload();
};
JohnB
  • 133
  • 1
  • 2
  • 13