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;
Related
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;
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
I am trying to implement what seems to be very simple JavaScript redirection, via the following rudimentary command:
window.location.href = "http://www.somesite.com";
So far so good, it works. I also can do it via the following method:
location.replace("http://www.somesite.com");
No problem here, it works again! The problem comes when I loose the protocol out of the string:
window.location.href = "www.somesite.com";
OR:
location.replace("www.somesite.com");
It just appends the new location to the current url:
www.currentsite.com/www.somesite.com
Of cause, that's not what I want. Is there any way to force the redirect?
One way is to use protocol-relative url like this:
window.location = "//www.somesite.com";
Or
window.location = "//somesite.com";
This way, it would redirect and browser itself will take care of figuring out protocol part eg http or https
Working Example
The protocol is required.
How else would the browser know whether
location.replace("mysite.pl");
was going to a Polish website or a Perl script on the current website?
You could do something like this to add http:// to the URL if it's not already there... although I can't think of a reason for not just including it yourself. Why complicate things?
function redirect(url) {
if(url.substr(4) != "http")
url = "http://" + url;
window.location.href = url;
}
redirect("www.google.com")
I'm having problem in redirecting at JS.
Here's the situation:
the current directory of the html code is : http://localhost:7927/MyWeb/Catch%20the%20bird%20game/GAME1.html
I want to change the directory to
'http://localhost:7927/MyWeb/games.htm'
I tried using window.location("games.htm"); but it will redirect to that directory:
http://localhost:7927/MyWeb/Catch%20the%20bird%20game/GAME1.html/games.htm
What's the solution? Thanks.
You can try:
window.location("../games.htm");
or:
window.location("/MyWeb/games.htm");
use
window.location = 'http://localhost:7927/MyWeb/games.htm';
or just this
window.location = '/MyWeb/games.htm';
I normally use
window.location.href = ...
or
window.location.replace(...);
Also check out this SO post.
You should use
window.location.href = "/myweb/games.htm"
I want to have the user click on a button and redirect him/her to a different site I have used the window.location before but for some reason it's not working this time. I'm sure the selector is correct as if I change window.location to alert it alerts every time, so I can't figure what I'm doing wrong.
$('#show').click(function(){
if($('select[name="customers"]').val() == '')
window.location.replace = 'http://google.com/';
});
You have to use either
window.location.replace("http://google.com"); // This is like http redirect
or
window.location.href = "http://google.com"; // This is like a link
You can't do window.location.replace= "http://google.com";
its a method, not a property.
window.location.replace('http://google.com/');
Shouldn't that be windows.location.href = ...?
Set just window.location.href. Like this:
window.location.href = 'http://google.com/';
try: window.location = 'http://google.com'