Refresh exact URL by JavaScript or jQuery - javascript

I am looking for a way to refresh the exact URL after ajax success. I try the following way:
location.reload()
history.go(0)
location.href = location.href
location.href = location.pathname
location.replace(location.pathname)
But in fact, I have another problem, the URL is https://example.com/electronic/?product_cat=mobile but after using the mentioned code, the URL will change and the URL will be https://example.com/electronic/
is there any way to keep ?product_cat=mobile after refreshing?

The easiest way of doing this will be
Get the current URL.
Redirect to it.
<script>
var current_url = window.location.href;
window.location.href = current_url;
</script>

Related

Why is window.location.href appending url again

In my code, I'm assigning the following:
window.location.href = "www.example.com/test";
But when the page actually loads, the browser URL is www.example.com/test/www.example.com/test. I'm not appending anything to the URL, and I'm not sure how its appending the URL again.
I think you're missing the "http" or "https" part. Have you tried the following?
window.location.href = "https://www.example.com/test";
or
window.location.href = "http://www.example.com/test";
Because you forgot the protocol. If you omit the protocol, window.location.href thinks you are trying to access a folder with the name of www.example.com, relative to the page you are currently on.
window.location.href="http://www.example.com/test/" will ensure that you access the external website www.example.com.
Hope this helps! :)
Check the way you are constructing the url, sometimes we miss the host, or enter the incorrect path
A safe way to change the URl is by making changes in the exisiting URL
first get the existing URL by
let exisitingURl = window.location.href;
now manipulate this url, for eg
exisitingURL = exisitingURL.replace('/auth', '/gateway');
now go to the url by
window.location.href = existingURL;

Redirect to url with some addition in the url

Suppose, i've my current url:
http://localhost:3000/contests
after the button press, i am going to redirect to the following url:
http://localhost:3000/contests?keywords=algo
My problem is, i want to redirect to the path with other additional information like:
http://localhost:3000/contests?keywords=algo&show=present
here, the &show=present is added to the redirected url. How can i do that using javascript?
How can i get the redirect url using javascript?
Do this in your button click.
var url=document.URL + "&show=present";
window.location.href = url;
Use document.URL to get the current url.
Hope this will help you!!
location.href = location.href + "?keywords=algo"
also check all the other possibilities with window.location object at MDN

Making A Dynamic HTML Page to Redirect to A URL Using JavaScript

I need to make A Dynamic HTML Page to Redirect to A URL Using JavaScript.As a beginner, i need your help...What i want to do is to redirect to a url through a html page.For example: suppose a page address is www.example.com/pages.html?http://yahoo.com/news so this pages.html page in this case will redirect a user to yahoo.com/news ....I know how to do this with PHP but i cant understand how i should do it with javascript and HTML . any idea? Thanks
This should do it:
function Redirect(){
var current = window.location.href;
var exclude = current.indexOf('?');
window.location = current.substr(exclude + 1);
}
setTimeout("Redirect()",5000);
You can use window.location.href="new-url.html"; to go to a URL with JavaScript.
To parse the URL, use var theURL=window.location.href.split("?")[1];
location.href = location.href.split("?")[1];

Javascript, redirect to a link retrieved by a function?

Right now I'm showing an href that redirects to a certain location, I want to redirect automatically to the same link but don't know how.
This is the link:
<script>
document.write(' Go ');
</script>
I tried with window.location but I'm messing somehow since I go literally to the name of the function.
How should I do it? Thanks
Setting window.location.href should redirect you to a new page, like this:
var url = geoip_city(); // url should be a valid url string
window.location.href = url;
You should use window.location.href to redirect. So assuming you have a function which returns an url:
function geoip_city() {
...
return 'http://example.com?foo=bar';
}
you could redirect like this:
window.location.href = geoip_city();

How to redirect to home page

How can I redirect a user to home page?
Example: mywebsite.example/ddfdf/fdfdsf and I want to redirect to mywebsite.example
However I want to do it without typing the static name. How can I do this?
document.location.href="/";
document.location.href="/";
or
window.location.href = "/";
According to the W3C, they are the same. In reality, for cross browser safety, you should use window.location rather than document.location.
See: http://www.w3.org/TR/Window/#window-location
(Note: I copied the difference explanation above, from this question.)
window.location.href = "/";
This worked for me.
If you have multiple folders/directories, you can use this:
window.location.href = "/folder_name/";
Can you do this on the server, using Apache's mod_rewrite for example? If not, you can use the window.location.replace method to erase the current URL from the back/forward history (to not break the back button) and go to the root of the web site:
window.location.replace('/');
maybe
var re = /^https?:\/\/[^/]+/i;
window.location.href = re.exec(window.location.href)[0];
is what you're looking for?
window.location = '/';
Should usually do the trick, but it depends on your sites directories. This will work for your example
strRetMsg ="<script>window.location.href = '../Other/Home.htm';</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", strRetMsg,false);
Put this code in Page Load.
var url = location.href;
var newurl = url.replace('some-domain.example','another-domain.example';);
location.href=newurl;
See this answer
https://stackoverflow.com/a/42291014/3901511
var url = location.href;
var newurl = url.replace('some-domain.example','another-domain.example';);
location.href=newurl;

Categories