This topic is locked
[SOLVED]

Javascript code embedded in page not working, but works when tested on webpages

8/8/2021 2:38:37 PM
PHPRunner General questions
B
bioman author

Hi,
I am trying to run a Javascript script that works in other html applications but does not work when I insert it into the PHPRunner page. I even copied the html and javascript of the phprunner page from the inspector and put exactly into a new document and it worked, but it does not work in my PHPRunner application. The event is an onload event.
Here is the script (I placed it at the end of the list page code right after {END body}:
<script>
<!---Membership login function to set sessionStorage LoginSuccess(call it upon logging in to phprunner membership)--->

window.onload = function LoginSuccess() {
// Check browser support
if (typeof(Storage) !== "undefined") {
// Store
sessionStorage.setItem("loggedin", "Yes" );
// Retrieve
document.getElementById("result").innerHTML = sessionStorage.getItem("loggedin");

} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}

}
</script>

Does anyone know why the script doesn't work? Are onload events handled differently in the phprunner applications than in standard html pages?

A
acpan 8/9/2021

Normally, if it does not work at Javascript Onload, i move it to header.php, and it often helps in my situation. Also this may help: window. onload

B
bioman author 8/22/2021

Thanks for the idea, but if I put it in the header.php, won't that affect all pages? I just want it to work once a person has logged in.

A
acpan 8/22/2021

Read this

You can use parse_url to restrict which page should be loaded with the js.

B
bioman author 8/22/2021

Thanks for the ideas. I think I figured out the problem. I was testing the script locally and the session storage variable would no longer be stored when I navigated to the web. Here is the script that ended up working when placed in a Javascript Onload event in the event editor:

//Membership login function to set sessionStorage LoginSuccess(call it upon logging in to phprunner membership)

// Check browser support
if (typeof(Storage) !== "undefined") {
// Store
sessionStorage.setItem("loggedin", "Yes" );
// Retrieve

} else {
alert( "Sorry, your browser does not support Web Storage...");
}

Thanks!

A
acpan 8/22/2021

Thanks for the update, i learn when i see how you fix it too! Good luck with your project!

HJB 8/22/2021

DISPLAYING A LIST OF USERS THAT ARE CURRENTLY LOGGED IN

May be I'm totally wrong in what you intend to achieve, yet inspired by "loggedin, yes",
the only sense making purpose would be the solution provided by the developers.