go to url in javascript doesn't work properly [duplicate] - javascript

This question already has answers here:
What is the definition of an absolute URL (fully qualified?)
(7 answers)
Closed 5 years ago.
I am trying to go to external url from my website with this code
var embed = "www.youtube.com";
console.log(embed);
window.location.assign(embed);
However, the webpage doesn't go to the the link in var embed but go instead to
Mywebsite/thepageofthatcode/www.youtube.com
window.location = "www.youtube.com";
window.location.href = "www.youtube.com";
I didn't get why is this happening?

Please ensure you add the http for external websites:
window.location.href = "http://www.youtube.com";

You should use the protocol before the url. Otherwise, the browser will think that it is a path.
var embed = "http://www.youtube.com";
console.log(embed);
window.location.assign(embed);

Add the protocol to make it work. Without the protocol it will search for www.youtube.com under your domain which is why it is redirecting to that way. Try
window.location.href = 'https://www.youtube.com';

Add the protocol to make it work. Without the protocol, it thinks that www.youtube.com is a part of your website.
This is the problem. But here's a better why to fix it:
// Use RegExp to test if embed already has the protocol
// If not, prepend it to embed.
if (!/^https?:\/\//i.test(embed))
embed = 'http://' + embed;
window.location.href = embed;
RegExp guide
URL Syntax

This solves the problem, only need to add the http part to the url
var embed = "http://www.youtube.com";
window.open(embed);

Related

How can I make a page that look for URL and create a text that refer to the URL?

So it's kind of a dumb question but I'm really wondering how I can make this :
user type www.mydomaine.com/something
page display : something
and it does with anything he type after the domain name
I've no idea how I could do that. I know I can get an info from an URL with jQuery but how can i remove the thing like index.html in the url? My guess would be with the htaccess?
Also, there won't be any other page but this with some design, how can I make sure someone doesn't go anywhere else but on the page that display what he wrote after the domain name?
I hope that's clear, thanks for reading and your answers !
Pierre
When creating an anchor tag and adding an href (or making a URL) I needed the URL to have a protocol (http or https), so I made a validation to add it, and then you can access the parameters of the URL easier.
Also, if you want to remove the / from the pathname you can use a .replace('/', '') when using parser.pathname
For removing index.html from the URL, you can split the path and get only the first element, or the ones you need f.e. parser.pathname.split('/')[0]
var myUrl = "www.mydomaine.com/something"
if (!myUrl.startsWith('http')) myUrl = 'http://' + myUrl;
var parser = document.createElement('a');
parser.href = myUrl;
console.log(parser.pathname);
// Other option
var theUrl = new URL(myUrl);
console.log(theUrl.pathname);
I used this as a reference.

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;

#Url.Content("~") not working for localhost [duplicate]

This question already has answers here:
Get absolute path of file on content
(5 answers)
Closed 6 years ago.
I'm trying to resolve the root url for the application in javascript using the following code:
var rootUrl = '#Url.Content("~")';
But the above code gives rootUrl as /.
What should I do to get the url as http://localhost:8000 on which my application is running.
You can get It directly from JavaScript:
var rootUrl = window.location.href;
alert(rootUrl);
The location property points to an object that contains information
about the URL of the currently loaded page.
You will get the same results with: window.location, location, location.href
Read more about window.location here
I also needed something similar a while back. My solution might not be the correct way of doing it but it was all that could find at the time. It worked for me and can work for you as well.
var rootUrl = "#Url.Content("~")";
Using the above code will give you this result:
var rootUrl = "/";
For what you are looking for you need to change your code to this:
var rootUrl = "#(new Uri(Request.Url, Url.Content("~")))";
Using the code above will give you the following result:
var rootUrl = "http://localhost:8000/";
I hope this helps.
#Url.Content("~/") is used to get your current application folder.
#Request.Url.Authority is used to get current host (with port)
So in able to get what you want you may want to mix them:
#String.Format("{0}://{1}{2}",Request.Url.Scheme, Request.Url.Authority,Url.Content("~/"))
Hope this helps!

Forcing Javascript Redirection

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")
​

document.location does not change the webpage in IE9?

I am trying to redirect to a different page in IE9 (9.0.3).
When I try to get/set document.location, or document.location.href, or window.location/window.location.href, I'm unable to do so. It fails without giving any errors.
I've tried to check whether the document and windows objects are set, and they are, so I have no idea why the location object is "missing".
I tried getting the document.URL and that works fine, but it's read-only.
Anyone know what the problem is or how to achieve this in a cross-browser way?
I was also experiencing the same problem but found that adding
window.event.returnValue = false;
above line in the javascript before the redirection resolved the problem.
See this: http://social.msdn.microsoft.com/Forums/en/iewebdevelopment/thread/c864ae63-66f6-4656-bcae-86b0018d70c9
Apparently it's a caching bug, you can solve it by appending a timestamp to the destination URL (that is, using a "unique" URL every time).
Perhaps your IE9 has some security restrictions in place that prevent JavaScript from directing URL's. window.location.href = "" should work normally on IE9.
Cache may be the reason, try:
location.href='something.php?tmp=' + Date.parse(new Date())
Hope it helps
You should use an absolute URL:
var url = '/section/page/';
var host = window.location.hostname;
window.location = 'http://' + host + url;
Where url is the relative path to your page.

Categories