Redirect to other window JS - javascript

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"

Related

non-www to www client side redirection with javascript

How can I redirect the following url in javascript :
http://example.com/?querystrings
=>
http://www.example.com
I want to redirect my url without www to www discarding the querystrings.
I know how to do it server side using mod_rewrite, I am working on a client side js app, where I need this.
Is this type of redirection possible with JS?
My code so far :
<body onload="addwww()">
<script>
function addwww() {
location.href="http://www.example.com";
}
</script>
it redirects the entire page to www with redirect loop error. how can I redirect only when there is no www in the url?
Thanks!
This script replace non-www to www:
if(!/\.?www./g.test(location.host)) {
location.href = location.href.replace("://","://www.")
}
As mentioned in the comment, you may try something like
if (location.href.indexOf('www') < 0)
location.href = 'http://example.com';
or
if (!location.href.indexOf('http://www'))
location.href = 'http://example.com';
as a start.
Use regexp to identify www:
if (!(/www/.test(location.href)))
location.href = 'http://www.example.com';

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

Why isn't this javascript redirecting?

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'

use variable with window.location.href

I was wondering how I would be able to execute a command such as this in javascript. I just want to store the url as a string in a variable and redirect them to it when the time comes -
var linkz = "http://www.google.com/";
window.location.href= linkz;
Why doesn't this work?
Thanks,
If you're using links this way (as mentioned in a comment):
Google
Then the problem is that you're not passing a string to your checkCk() function.
Try this:
Google
The code you used for window.location.href should work.
At this point, I don't see why you'd use javascript if all you're doing is replacing the default behavior of the link.
I've just tried on my local machine, and it works:
<script>
window.onload = function(){
var google = "http://google.com";
window.location.href = google;
}
</script>
Redirecting to google...
Copy this to new file, call it for example redirect.html and open it in your browser.
Update:
<script>
var redirect = function(new_place) {
window.location.href = new_place;
}
</script>
<a href='javascript:redirect("http://google.com")'>Go to google</a>

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