0

I'd like to have a click on a button reload the page and change the text inside the button.

For reload I got:

Javascript:

function reloadPage(){
window.parent.location = window.parent.location.href;}

HTML:

<button type="button" onClick="reloadPage()">Start</button>

<script src="reloadpage.js"></script>

After click, the text in the button must change from "Start" to "Try Again!"

How can I do this?

Thank you.

  • 1
    Kayla: Take a look on this case: http://stackoverflow.com/questions/5004978/check-if-page-gets-reloaded-or-refreshed-in-javascript – Joel Hernandez Aug 18 '16 at 16:25

2 Answers2

1

You could do this by adding a query string on reload, and then have a script that detects the query string and changes the text of the button onload.

JavaScript:

function reloadPage(){
window.parent.location = window.parent.location.href+"reload=true";}

function checkQString(){
// get query strings and store reload qstring in variable
if (reload) {
    document.getElementById('btn').innerHTML = 'Try Again!';
}

HTML

<button id='btn' type="button" onLoad='checkQString();' onClick="reloadPage()">Start</button>

<script src="reloadpage.js"></script>
  • Agreed with your answer. I would like to add that it is recommended to determine the text on server-side based on get parameter instead of generating it and then overriding it. – Lajos Arpad Aug 18 '16 at 16:32
  • This [link](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) will show how to get QStrings – Elias Marcopoulos Aug 18 '16 at 16:33
  • Elias, I understand your solution, upvoted it, but criticized it, since the part where you assign 'Try Again!' to innerHTML is an overkill. You should do that on server-side before you send this to the user instead of generating the markup and then searching for 'btn' and overriding its innerHTML. – Lajos Arpad Aug 18 '16 at 16:37
  • Thank you for the upvote and the criticism. However, I am uncertain where in the OP's question there is an indication of an independent server? To me it seems like OP is simply dealing with client side code. I do see that, if there were a server, it would be much better to use your suggested solution. @LajosArpad – Elias Marcopoulos Aug 18 '16 at 16:41
  • Elias, that's a good point. In fact I wanted to write an answer, but yours was sent earlier, therefore I upvoted it and criticized it. In fact, if there is no server-side, then something like your code should be used. If there is a server-side, then my suggestion should be used. As a result, I believe you should edit your answer to reflect the result of our conversation and then it will be of very high quality. Now it is "just" of high quality :) – Lajos Arpad Aug 18 '16 at 18:27
0

You can use Cookies. With js you can use https://plugins.jquery.com/cookie/ Or can use server side cookies

napalias
  • 1,145
  • 1
  • 10
  • 21