How to run a Javascript program automatically after page refresh? - javascript

I am wondering if it is possible to have my program which ends with a page refresh, automatically run again after the page refreshes.

call the function(s) you want to run.
function doBusiness() {
// the business
}
doBusiness(); // leave the program where it is but make sure this is outside of any function that isn't called right away
If you don't want to do business the first time the page loads, look into sessionStorage, good starting point is mozilla doc. cheers

You can use:
<body onload="yourLoadingFunction()">
or
document.onload = function(){
//your code here
}
Any of these functions should be included in your tag in your page.

Related

Javascript From Console - Load a Few Pages, Then Run Function Per Page Load [duplicate]

I'd like to be able to call a jquery function once window.location has completed loading a URL. Is this possible? I can't seem to find anything online about this.
for instance:
if(parseInt(msg.status)==1) {
window.location=msg.txt;
alert("This URL has finished loading")
}
Thanks,
-Paul
You can either use window.onload of the destination page (if you have access to modify the code of that page), or you can use window.onunload to have the alert be launched when unloading the current page. You cannot execute code on the current page after the new page has been loaded.
Yes.
This page demonstrates onload/onunload behavior.
<html>
<head>
<script>
window.doUnload = function(){
alert("Here!");
}
window.doLoad = function(){
window.location="http://www.google.com";
}
</script>
</head>
<body onload="doLoad();" onunload="doUnload();"></body>
</html>
After a user logs in for the first time I need to load my index page to initialize everything but then need to forward them to another page for profile completion.
use window.location to redirect the user to your index, adding a query parameter (something like window.location=index.php?firstLogin=true ) and on your index redirect (using javascipt http 300, header() or whatever you are using) to the profile page after it ends loading if the parameter is set
Iframe
One (ugly) method you could use is to instead of using window.location, clearing the body, adding an iframe with the relevant path and listening to its onload function.
After that you can run code inside the iframe as long as it's not cross-site scripting.
I use this method to perform small automated scripts, that can't really on third-party plugins.
Ajax
Another method might be using ajax to load the page/body content. Then replacing your body with the newly loaded body and start executing the next functions.

Refresh and call function in script

So I have a function in a script called start() and at the end of the start() function I will ask if the user if s/he wants to play again. If they do want to play again, i want the page to refresh and im doing this using location.reload() and then i want to start from the beginning of the start function again. However, when i simply do this...
location.reload();
start();
it doesn't refresh the page. I was wondering if there is another way for me to go about doing this?
More Info:
this is a black jack game that i am creating for learning purposes.
start function is called when a button is clicked.
in the start function, the dealer and player cards will be shown and then the player has option to either hit or hold.
I wanted all of the things done in the start function erased on the webpage and i done it by refreshing the page(it was the simplest way to me). and then i wanted to call the start function again when the page is refreshed and the user selects okay on the confirm dialog box that he wants to play again.
**EDIT: http://jsfiddle.net/MAbgW/7/ my code is here. pls help if you're free. seems like an easy fix i think i might just be saying it in a confusing way.
Have you tried...
location.reload(true);
Alternatively, you could do...
location.href = location.href;
This would simply send them back to the same URL.
You don't have to reload page. Restart your function in a loop like this:
function start() {
do {
alert('The Game is On!');
} while (confirm('Do you want to play again?'))
}
start();
Do not use page reload as a means of reinitializing data - rather initialize them at the beginning of your start function (first thing inside of do block in code above)
Demo with data init: http://jsfiddle.net/e9W6q/1/
This should do it. Reloads the page and stops the rest of the start() function, unless the user is done.
EDIT:
function start(){
alert("Going...");
var cont = confirm("Do you want to continue?");
if(cont){ location.reload(); end; } // notice the end
alert("Let me know if you see this when you select OK");
}
start();
This code will work: http://jsfiddle.net/MAbgW/9/

reload php page with javascript

I have drawn a chess board in a php page. Every piece is set as draggable, and every tile as droppable. Once a piece is dropped on a tile, I'd like to reload the php page so the board can be drawn anew along with new positions.
How can I do that: reloading the php page with javascript, without displaying a window asking for confirmation such as "To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier. ->Cancel; Resend" ?
Or perhaps there are better solutions?
If you want to avoid having refresh reporting data (for any reason, including the user clicking the reload button) then use the POST-REDIRECT-GET pattern (http://en.wikipedia.org/wiki/Post/Redirect/Get). Read that, it will explain what to do.
Quick solution: you could try:
window.location.reload(true); //true sets request type to GET
Use GET, instead of POST and the dialog box you are getting will go away.
Good luck!
Make use of
window.location.reload();
will refresh automatically
<script>
var timer = null;
function auto_reload()
{
window.location = 'http://domain.com/page.php'; //your page location
}
</script>
<!-- Reload page every 10 seconds. -->
<body onload="timer = setTimeout('auto_reload()',10000);">
reference http://davidwalsh.name/automatically-refresh-page-javascript-meta-tags

how to call a javascript function on only first page loading only

I have a javascript function "myFunction()" and i need to call this function on First page load only not for all page.
how can i do it.
I will appreciate a lot your help.
thanks
The best is to store in localStorage the fact that you already visited the site (to be precise it's the "origin").
You can put this code in all your pages :
<script>
if (!localStorage['done']) {
localStorage['done'] = 'yes';
myFunction();
}
</script>
If you want to show the page again for next session, replace localStorage with sessionStorage.

Detect first page load with jQuery?

I need to detect the first time a page loads in jQuery so that I can perform some actions only when the page loads the first time a user navigates to that page. Similar to server side code page.ispostbasck. I have tested $(document).ready and it fires every time the page loads so this will not provide what I need. I have also tried the jQuery Load function - it also fires every page load. So by page load an example is that I have an HTML input tag on the page of type button and it does not fire a postback (like an asp.net button) but it does reload the page and fires $(document).ready
Thanks
You will have to use cookie to store first load information:
if (! $.cookie("cookieName")){
// do your stuff
// set cookie now
$.cookie("cookieName", "firstSet", {"expires" : 7})
}
Note: Above example uses jQuery Cookie plugin.
An event doesn't exist that fires only when the page is loaded for the first time.
You should use jQuery's .ready() event, and then persist the fact that you've handled a first time page load using your method of choice (i.e. cookie, session variable, local storage, etc.).
Note: This method will never be fool proof unless you can store this information at the user level in a DB. Otherwise, as soon as the user clears their cookies, or whatever method you choose, the "first time loaded" code will fire again.
I just ran into this problem and this is how I handled it. Keep track of the first time the page loads by using a variable initialLoad:
var initialLoad = true;
$(document).ready(function() {
...
...
...
initialLoad = false;
});
Then in other functions, you can do this:
if (initialLoad) {
//Do work that is done when the page was first refreshed/loaded.
} else {
//Do work when it's not the initial load.
}
This works well for me. If the user is already on the page and some jQuery functions run, I now know if that user just loaded the page or if they were already on the page.
The easy solution is to use jQuery ‘Once’ plugin
$(element).once('class-name', function() {
// your javascript code
});

Categories

Resources