How to run a javascript function when ANY page is loaded - javascript

I am doing some maintenance work to a fairly large existing site written in PHP & JavaScript. I am trying to set up a "time out" so that the user is automatically logged out after a specified period of time. I think I have the code that will "expire" the users session, but what I need is a way to run a specific javascript function whenever ANY of the pages in the existing system are loaded. I'd rather not manually add a call in each page as this would take forever and would require even more testing.
Is there a way to add a javascript function call to the window or some other part of the DOM to get called whenever a page is loaded?
Thanks for any help!

There are many ways to achieve this. BUT, you will have to first include a reference to the javascript file.
Then, you can, for instance, use jQuery to detect that the DOM is loaded and ready to call a function of yours.
On a side note, can I ask you why you need to call a javascript function? There are probably other ways to do that, like a listener on your server that redirects to a logout page when a session expires.

Write the javascript in a .js file, host it on your server, and link to the .js from all the pages. While this does not apply a global rule, it is the only way I can think of and it won't be a problem for testing as the code will be from one source.

No.
You have to have the javascript function load into every page. But you just have to write it once and then include like:
<script src="logout.js"></script>
and then you'll need to set the timer for the logout
<body onload="setLogoutTimer()">
But in order for every page to have it you either need to explicitly place it in every page.

That depends to your site script. If you're checking your session data on every request this could be done easily.
Add time data for a session's last move, append it to a js script which controls that If it's time to end the session or not. And take action(js redirect or an ajax request)

Related

Keep timer (setInterval) running while reloading page

Once I load a webpage, I insert some Javscript via the console. I was wondering if it's possible for me to, using either Javascript or jQuery, reload the page (not from cache) while keeping a setInterval that I have running. I'm familiar with location.reload(), but that terminates it.
When you reload a page, the entire page context including all running JS is completely destroyed. You cannot keep a setInterval() running while its host page is reloaded.
You can create a signal for the new page to start the interval going again itself using a cookie, query parameter or local storage value (query parameter is probably the most appropriate). If you go this way, then you need to code the page to look for a specific query parameter and if it finds it, then the page should start the designated setInterval() itself. You can even pass some data in the query parameter (such as how much more time until the next interval should fire, etc...).
Another option is to not actually reload the page, but instead to refresh the content manually by getting new content via an ajax call and then inserting it into the current page. This allowed the current page context and running interval timers to continue running.
Not possible unless you fetch the page using Ajax request, then replace the body while the setInterval Is working

How to detect if a user leaves a page in PHP

There is this function which i want to execute in case the user leaves a particular page. This function will basically change all of the data within a certain column in my database. So in case the user leaves this page, i want this function to be performed by the system. Is there a way to detect if the user already left the page.
Thanks!
With 100% reliability, no, it's not possible. Since leaving a particular page is a client-side operation, you have 0 control over what the client does. You can register an onbeforeunload handler via Javascript and hope that the client browser supports it. But again, support for this isn't universal.
You can't do this in php but you could use the javascript function onbeforeunload, here an example
<script type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
</script>
You can't do that using PHP since it's a server-side language.
You will need to use JavaScript.
Since server-side php only runs the server script and then sends the result to the browser you can't do this with php alone. You could send an ajax request to your php script with the onunload event using javascript, and then run the php script in response to that request.
To cover the cases where the user leaves a particular page to navigate to another one inside the same website, you can try to achieve this using page IDs, store the ID of the "page_to_be_left" in a session var, and check if the current page ID matches the one stored in the session var.
Your function could then be triggered if there is no match.
You need to use the javascript function
onbeforeunload

passing parameters from js to .aspx page using windows location

I can pass parameters from javascript function to an aspx file. I use windows.location.href for that.
When the execution gets to the aspx page, it runs and it tries to call multiple WCF services using those parameters. But the moment it calls the first one, the IE window on the back takes me to the root view that lists all available aspx pages.
i've tried setting a breakpoint on the page load that is receiving those parametrs. I try to step through the codes, it will let me do that just fine but when it calls the first WCF service, it will do the above to the IE window BUT it will still let me continue steppng through the code after that.
IE window should be waiting for me until I am done executing code on the server, right?
It has something to do with the combination of WCF calls & JS window.location.href statemetn. I know that for sure. It consistently is doing that at a specific line of code (the first WCF call).
I don't see this behavior if for instance, I try to pass the parameters in a session variable coming from another .aspx page.
Has anyone seen this behavior? do you know what is going on?
Thanks
--- REPHRASED---
thank you for the feedback.
hari, I am doing this from my local machine running VS 2008. I do use forms authentication.
Maybe you didn't get my questions, because I didn't phrase it properly like Diodeus thinks, so let me try again:
I collect input values form one page using JS. Then I need to pass these parameters to an aspx page using windows.location.href.
I set up a breaking point at the page load event to make sure that execution is reachign the aspx page. and it does.
This page successfully receives those variables and does more processing like calling external WCF services. At this point the IE window is locked and becomes unviewable because the execution is still taking place. This is normal (is this part that throw you off?).
The moment I start executing a call to the WCF service, the IE window finishes loading and it takes me to the direct browsing WHILE I am still debugging inside visual studio (like a line of code beomes highlighted in yellow). I can continue stepping throw using F10 or F11, but my IE window on the back is done!? It should continue waiting for me until all lines of codes finish processing.
I hope this helps. Pls let me know if it does and be able to help me figure it out.
Disable directory browsing for that website (at IIS), and set a default page for the website (also at IIS).

Call javascript function when consuming event in codebehind

I have a aspx page that, when loading, will initialize a class called, Bill :)
When Bill is initialized, the aspx page will wire a function to a handler of Bills.
When that function in Bill is fired, I would like to call a javascript function in the aspx page, but that is where I am having difficulties...
I have tried ClientScript.Register and all of that, but nothing is seeming to work. As of now, I am simple trying to get the function to fire an alert.
I have a feeling that I am missing something critical in the understanding of this, so any information would be greatly appreciated.
Thanks.
Is all of this happening in javascript, or is some of this server-side code? Javascript is client-side only, so if you are running server-side code, you can't execute the javascript on the server. You can, however, register script to run when the page renders in the browser (be careful of this, if you are opening another window, actions like this will be blocked by the browser unless the user directly initiates the action by clicking a button or something similar).

Javascript function execution on link click?

I have a link, that when a user clicks on it, it loads a different page as normal but also executes a JS function that autofills a specific text-box on that different page. Is it better practice to use Jquery or Javascript to do this? How can I do this using either one of them?
You can't do this from the source page.
It's a security feature. Imagine if you wrote a JS function that went to an online banking page and auto-filled a bank transfer using the user's current cookie. That's why you can't.
If you control the other page then the sequence you can use is:
Save data to the server;
Go to the new page with a JS redirect;
The new page is loaded from the server;
While loading th epage the data that was saved from the server is retrieved and used to populate the text box.
So it can be done from the server but only if you save it there. The only way of doing that is using Ajax.
An alternative approach is:
Instead of a JS redirect, submit the page back to the server;
The server saves whatever data it needs to;
The server sends back an HTTP redirect to the new page;
The new page uses the saved data to construct the new page with the populated text box.
At the end of the script add return false;. This will make the page run the script without redirecting the page.
Edit: (after saw your edition).
Is it better practice to use Jquery or Javascript to do this? How can I do this using either one of them?
jQuery is a javascript library, this it doesn't matter if you use plain javascript or use jquery as long as you happy with the result.
And about what you say that you successfully manipulated a page fro the redirecter page... I don't see how it possible.

Categories

Resources