Page reload goes back to index.php - javascript

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';?

Related

setTimeout placed in header , how can i restrict to run 1 time

How can i make sure this doesn't execute again. I am redirecting all my pages to a single page on my site. It keeps refreshing/reloading that page because the script is placed in the header and must be.
setTimeout(function(){
window.location="mysite/page";
},1000);
You can add an hash to your url when you refresh the page:
<html>
<head>
<script type="text/javascript">
var loc = window.location.href;
if (loc.indexOf ("#DoNotRefresh") < 0) {
setTimeout(function(){
window.location="#DoNotRefresh";
},1000);
}
console.log(new Date());
</script>
</head>
Cant you just check to see if the location is already set?
if(window.location !== 'mysite/page')
{
setTimeout(function()
{
window.location = 'mysite/page';
}, 1000);
}

How to reload page once only without repeating loading

I have the following code to reload page only once after submitting some data using JQuery.
code to reload page :
update: the url here is not ending with '?' because it has parameter value
for example: http://localhost:49208/UserView.aspx?id=12
var url = window.location.href;
if (url.indexOf('?') > -1)
{
window.location.href = url;
}
The problem here is that page reloading does not stop?
The reason it won't stop reloading is because you aren't changing the conditions of the url; so if the if statement is ever true, it will happen again and again.
If you want to reload the page, just use window.location.reload();
Try this logic.
if (url.indexOf('?') == -1) {
url = url + '?';
location = '?';
location.reload(true);
}
Easiest way is to add a flag variable so that javascript can check whether the page is reloaded previously.
var url = window.location.href; // get the current url of page into variable
if (url.indexOf('?') > -1) { // url has a '?'
if(url.indexOf('reloaded') < 0){ // url does not have the text 'reloaded'
url = url + "&reloaded=true"; // add the word 'reloaded' to url
window.location = url; // "reload" the page
}
}
If you want to reload the page only once, use the following method:
if(!window.location.hash) {
window.location = window.location + '#loaded';
window.location.reload();
}
if(!window.location.hash)
{
window.location = window.location + '#loaded';
window.location.reload();
}

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();.

Refreshing page after function

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';

Refresh the page after first load but not if I refresh it again?

I want to reload my html page just after it loads for the first time, but not to reload if page is refreshed. What I have done is:
window.onload = function() {
if(!window.location.hash) {
window.location = window.location + '#loaded';
window.location.reload();
}
}
But with this approach if I refresh the page it gets reloaded which I don't want. Are there any solutions to avoid this? Thanks in advance.
Use localStorage to achieve this.
Try:
alert("page load")
if(localStorage.getItem("reload") != "1"){
localStorage.setItem("reload","1");
window.location.href = window.location.href;
}
else{
localStorage.removeItem("reload");
}
This will do it, but how do you prevent reloading on all browsers but Firefox?
if (window.location.href.toLowerCase().indexOf("loaded") < 0) {
window.location = window.location.href + '?loaded=1'
}
Try setting the cookie and check

Categories

Resources