In my javascript code, in some point, i need to refresh the window (user has uploaded new image but in page still can see it)
i am wondering why is
location.href = location.href
not refreshing the window?
To refresh the window try:
window.location.reload();
If you are wanting your to redirect your page, use:
window.location = location.href;
Or, if it is a simple case of refreshing the page:
window.location.reload();
if you really really need to refresh the page you should do
location.reload(true)
Related
I want to reload a page like so :
window.location.href = url;
But given my url contains an anchor the previous instruction won't do anything.
How to deal with this issue ?
Thank you for the great help.
Just use:
window.location.reload();
You can do location.replace("https:/rickrolled.com");
So, to redirect, you can do location.replace(location.href);
Or just do location.reload();
If it doesn't work, please send me a JSFiddle of your project.
I'm trying to redirect all my site mobile traffic to one page.
so I added redirect code in header.
<script type="text/javascript">
<!-- if (screen.width <= 699)
{ document.location = "/page/mobile/";
}
//-->
</script>
The problem is that after redirecting script continuous executing because header is the same on the whole site. so it's refreshing the page over and over again.
I want javascript to check for the /page/mobile/ in url and if it's there, do not execute redirect.
How can I achieve this? Thanks!
Do this.
if ( window.location.pathname !== "/page/mobile"){
window.location.href = "https://[your-host-name]/page/mobile";
}
So you can use window.location.pathname to access your path after host (and port)
https://developer.mozilla.org/en-US/docs/Web/API/Window/location
check window location in your if condition
if (window.location.href.indexOf("/mobile/")>-1){
console.log("You are on mobile page")
}
I am trying to refresh the page with this statement in external javascript file after some processes.
window.location.href = window.location.href
It perfectly reload the page but I want to scroll to the specific location after reload.
So, I put this on the page.
<a name="uploadImgSection"></a>
And change the javascript to
window.location.href = window.location.href + "#mypara";
This code doesn't reload/refresh the page either
window.location.hash = "#uploadImgSection";
window.location.href = window.location.href;
When I do that, it doesn't reload the page at all. Is there any way I can reload the page with scroll bar position?
window.location.href += "#mypara";
location.reload();
First add the hash, then reload the page. The hash will remain there, and the page gets reloaded.
Tested, works.
ps: If a hash already exist in the URL, you may want to directly change it via location.hash instead of .href.
I wanted to update this question because I found that using window.location.href += "#mypara" will keep appending the parameter to the url even if it exists. I have found the better way is to use
window.location.hash = "#mypara"
location.reload();
This is really an old post, I would still try and answer with right combination of answers.
location.reload() and location.reload(true) works like F5 on browser. This will post all the form data back to the server if previously load had done it.
location.href will not refresh the page until there is a URL change recognized by the browser. change in hash (#paramname) doesn't qualify for URL change and hence just doing
location.href+="#paramname" will not work.
So, location.href+="?anything#paramname" should reload the page as a new request as ?anything is change in the URL.
var loc = location.hash;
location.hash = " ";
location.hash = loc;
Try this
var urlString=window.location.href;
var url=urlString.split("#")[0];
window.location.href = url + "#mypara";
This worked for me:
var tabValue = document.URL;
window.location = tabValue.substring(0, tabValue.lastIndexOf("#"));
window.location.hash = "#tabs-3"; //Whatever is your hash value
location.reload();
This work for me
window.location.href = baseUrl + "#/m/team";
location.reload();
This worked for me
$('body').on('click', '.className', function () {
var data = $(this).attr('href');
alert(data);
window.location.reload();
});
I want to do that when I clicked on link (for example test.com/page?=test) my tab in the broswer open the link but without reload or refresh the page and change the content to the page content.
How can I start it? I know PHP, but its the first time that I try to do this.
Well, i doubt there is any easy way for this.
https://www.w3schools.com/xml/ajax_intro.asp
This might help.
You can use HTML5 History API
document.body.addEventListener('click', function(event){
if(event.target.tagName === 'A'){
event.preventDefault();
history.replaceState({}, "Page Title", event.target.href);
}
})
I want to use JS/jQuery to reload the current page and jump to an anchor.
I've tried using:
window.replace('currentpage/#msg'+.message_id);
And
window.location.href ='currentpage/#msg'+.message_id;
Both change the url in the address but fail to reload the page.
Any other ways to do it?
call this after changing url :
window.location.reload(true)
There are more ways to do it: http://www.phpied.com/files/location-location/location-location.html :)
Try this way
First get the current URL
var CurUrl = window.location;
Now change the redirect URL in the browser without further'
history.pushState(CurUrl, "OldPage", "NewPage");
Now the browser URL has changed but the page was not redirected and get the URL again'
var UrlRedirect = window.location;
here you can write your code
Finish redirecting
window.location = UrlRedirect;
Another way to force the redirect is to make sure that the URL does change, by adding in a random element. I often add a timestamp to the end of a URL.
This has the added benefit or drawback that each page load will look unique to caching systems/etc. You may or may not want this.