Refreshing page after function - javascript

Im using php + Jquery.
I want to reload my page at the end of "myFunction".
This is what i have currently:
<script>
function myFuntion(){
var x = prompt("Enter Value", "0"); //This prompts when calling myFunction()
$("#myDiv").load("config/calcX.php"); //This loads"config/calcX.php" in a Div;
location.reload(true); //Does not reload my page...
}
</script>
is there something wrong with myfunction?

There are several ways:
location.reload()
history.go(0)
location.href = location.href
location.href = location.pathname;
location.replace(location.pathname)
location.reload(false)

You can try this
window.location.href = 'yourpage.com';

Related

Can we have code after location.reload(true)?

I have a function that does a reload from server with:
location.reload(true)
Currently, any code I have after this line is not executed.
I'm curious if there is a way to be able to have more code run after the reload?
The best way to do this would be to write a function that is called if there are certain "GET" variables set and to check them on each run such as mypage.html?reload=true
Or even hash locations: mypage.html#reload
And you can compare like so:
$(document).ready(function () {
if (window.location.hash == '#reload') {
onReload();
}
});
function onReload () {
console.log('test');
}
function reload () {
window.location.hash = 'reload';
window.location.reload();
}
Just call the reload(); function.
Output (in console): test
And then you can just redirect people away from the hash #reload if you don't want your function to run if the page is refreshed again: window.location.hash = '';
why you need execute something after reload the page?
If you need to do something after reload, so you can pass some parameter to the page and check this to execute something additional. You can redirect to the same page and pass a complete url
var url = window.location.href;
url += '&param=1'
window.location.href = url;
So, when your page reload, you can check a parameter by GET and do whatever you want. I hope this trick help you!
________________METHOD 1: WITH PHP_________________
1) reloading with a parameter
2) getting that parameter with PHP
For example:
window.location.href += '&reloading=1';
//it's equivalent to:
//window.location.href = window.location.href + '&reloading=1';
And then:
<?php
if (isset($_GET['reloading'])) {
echo '<script>pageReloaded();</script>';
}
?>
Since PHP will be executed first, that little script (the function call) will be writen and then executed in javascript, so pageReloaded() function will be called.
A full example would be:
<?php if (isset($_GET['reloading'])) echo '<script>pageReloaded();</script>'; ?>
<script>
function reloadNow(){
alert("I'm about to reload");
window.location.href += '&reloading=1';
}
function pageReloaded(){
alert("I've been reloaded!");
}
</script>
<button onclick="reloadNow()">Press to reload</button>
________________METHOD 2: NO PHP, ONLY JAVASCRIPT_________________
Using SessionStorage
1) Set SessionStorage.
2) Reload
3) Get that SessionStorage
For example:
<script>
function reloadNow(){
alert("I'm about to reload");
sessionStorage.setItem('reloading',"true");
window.location.reload(true);
}
//when loading:
if (sessionStorage.getItem('reloading') === "true") {
sessionStorage.clear(); //so it doesn't trigger next time
alert("I've been reloaded!");
}
</script>
<button onclick="reloadNow()">Press to reload</button>

window.onload function not running on refresh

I have an window.onload function that does not run when a user manually refreshes the page. I have noticed that on the page refresh, the URL is appended with a # at the end, but I don't know if that has anything to do with the error. The function correctly executes when first loaded, but not after a refresh.
window.onload = function() {
alert("HERE");
var a = document.getElementById("link1");
a.onclick = function() {
var current = window.location.href;
alert(current);
if (current.indexOf("&page=") != -1) {
current = current.substring(0,current.indexOf("&page="));
}
var nextPage = current + "&page=link1"
window.location.replace(nextPage);
return false;
}
}
UPDATE: It seems as though it is working in Chrome, but not Safari.
Also, additional information, my a tag looks like this:
<a id='link1' href='#'>Link 1</a>
try using :
$(document).ready(function() {
});
instead.
check if it helps..

window.location.href not being assigned / redirected

This is a followup to a prior question that you guys answered a few weeks ago. There is HTML in a WordPress.org app which I cannot change. A section in this HTML contains text that is hyperlinked to someURL. I want the URL to redirect to my referrer page in order to return to whatever page I cam from when I click on this link. The problem is that something is blocking the assignment of my referrer URL string to HREF. I stepped through this function a hundred times in the Chrome debugger, and tried various thing (see below). HREF simply will not change, not matter what. Any idea why? here is the code, and thank you for any any help on this:
window.onload = function () {
document.querySelector(".button.wc-backward").onclick = function() {
var URLstring = document.referrer; // works fine
window.location.href = URLstring; // ref unchanged
setTimeout(function () {
window.location.href = URLstring; },100); // same result
return false;
}
}
There is no issue with you jscode, I have added it to here:
Example on github
And it is working. (You will be redirected back to stackoverflow).
<html>
<head>
</head>
<body>
<script>
window.onload = function () {
document.querySelector(".button.wc-backward").onclick = function() {
var URLstring = document.referrer; // works fine
window.location.href = URLstring; // ref unchanged
setTimeout(function () {
window.location.href = URLstring;
}, 100); // same result
return false;
}
}
</script>
the link
</html>
You code is wokring... do you like to try this one too
window.location.assign(URLstring)

How to set window.location.href

I have a button on the page which reloads the page based on some value selected. The button's onclick event of calling load_newurl(param).
function load_newurl(param) {
var url = window.location.href;
var index = url.indexOf("&test=");
if (index>=0) {
url = url.substring(0, index);
}
url = url + "&testrun=" + param;
window.location.href = url;
window.location.reload();
}
Above is my function to reload the page. However, window.location.href never gets changed. Do anyone know why? Am I doing something wrong...?
Thank you in advance.
Don't call reload.
This should work, provided there's nothing else wrong with your code.
function load_newurl(param) {
var url = window.location.href;
var index = url.indexOf("&test=");
if (index>=0) {
url = url.substring(0, index);
}
url = url + "&testrun=" + param;
window.location.href = url;
}
Just remove the call to
window.location.reload();
it should work.
Your code works fine, but while the href is about to be set, the reload() refreshes the current page and its href stays the same.
Just try your code ommiting window.location.reload();.

Page reload goes back to index.php

I'm using this script within my website:
<script>
//refresh the page once to load slideshow correctly
setTimeout(function(){
if(!window.location.hash) {
window.location = window.location + '#loaded';
window.location.reload()}}, 1000);
</script>
When it is executed the current URL (http://.../slider/Cycle2/cycle2-tile.php?selection=3%2C4)) is not realoaded and appended with '#' but the index.php ((http://.../#loaded)) is loaded instead. Why does this happen and how can I fix it?
Can you try this?
<script>
//refresh the page once to load slideshow correctly
var currentLocation=window.location.href;
setTimeout(function(){
if(!window.location.hash) {
window.location = currentLocation + '#loaded';
window.location.reload()}}, 1000);
</script>
Did you mean:
window.location.href = window.location.href + '#loaded';
instead of
window.location = window.location + '#loaded';?

Categories

Resources