Force reload of page with URL containing an anchor (#) - javascript

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.

Related

window.location with hash returns to the top of the page [duplicate]

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

How to change page url and page content without reload the page?

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);
}
})

onclick page go to homepage without any absolute path

I want to click on link and I want to go to the page on home page by it self
<a onclick="javascript:GoToHomePage()" href="javascript:void(0)">Home page</a>
function is
function GoToHomePage()
{
window.location = 'default.aspx';
}
but i want the url of page www.testtest.com instead of www.testtest.com/default.aspx
I can't even give the absolute path like www.testtest.com as it's a portal and same page coming in few more portals.
following code will take you to '//www.testtest.com/'
function GoToHomePage()
{
window.location = '/';
}
Try this:
window.location = '/';
Executing this code will send your visitor directly to the root of your website.
It looks like you need
window.location = '/default.aspx';
or
window.location = '/';
(not sure of your goal)
The '/' means "the root of the domain".
Add / before default.aspx and it should take you to home page
function GoToHomePage()
{
window.location = '/default.aspx';
}
I would strongly recommend to use href instead of onclick due to various obvious reasons:
Home page
It's working for me, as suggested by #Olly Hodgson:
Home page

location.href = location.href wont redirect

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)

JS redirect/window replace doesn't work if url is the same (but anchor added)

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.

Categories