Javascript How to check if the page opened is Home Page [duplicate] - javascript
All I want is to get the website URL. Not the URL as taken from a link. On the page loading I need to be able to grab the full, current URL of the website and set it as a variable to do with as I please.
Use:
window.location.href
As noted in the comments, the line below works, but it is bugged for Firefox.
document.URL
See URL of type DOMString, readonly.
URL Info Access
JavaScript provides you with many methods to retrieve and change the current URL, which is displayed in the browser's address bar. All these methods use the Location object, which is a property of the Window object. You can read the current Location object by reading window.location:
var currentLocation = window.location;
Basic URL Structure
<protocol>//<hostname>:<port>/<pathname><search><hash>
protocol: Specifies the protocol name be used to access the resource on the Internet. (HTTP (without SSL) or HTTPS (with SSL))
hostname: Host name specifies the host that owns the resource. For example, www.stackoverflow.com. A server provides services using the name of the host.
port: A port number used to recognize a specific process to which an Internet or other network message is to be forwarded when it arrives at a server.
pathname: The path gives info about the specific resource within the host that the Web client wants to access. For example, /index.html.
search: A query string follows the path component, and provides a string of information that the resource can utilize for some purpose (for example, as parameters for a search or as data to be processed).
hash: The anchor portion of a URL, includes the hash sign (#).
With these Location object properties you can access all of these URL components and what they can set or return:
href - the entire URL
protocol - the protocol of the URL
host - the hostname and port of the URL
hostname - the hostname of the URL
port - the port number the server uses for the URL
pathname - the path name of the URL
search - the query portion of the URL
hash - the anchor portion of the URL
origin - the window.location.protocol + '//' + window.location.host
I hope you got your answer..
Use window.location for read and write access to the location object associated with the current frame. If you just want to get the address as a read-only string, you may use document.URL, which should contain the same value as window.location.href.
Gets the current page URL:
window.location.href
OK, getting the full URL of the current page is easy using pure JavaScript. For example, try this code on this page:
window.location.href;
// use it in the console of this page will return
// http://stackoverflow.com/questions/1034621/get-current-url-in-web-browser"
The window.location.href property returns the URL of the current page.
document.getElementById("root").innerHTML = "The full URL of this page is:<br>" + window.location.href;
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<h3>The window.location.href</h3>
<p id="root"></p>
</body>
</html>
Just not bad to mention these as well:
if you need a relative path, simply use window.location.pathname;
if you'd like to get the host name, you can use window.location.hostname;
and if you need to get the protocol separately, use window.location.protocol
also, if your page has hash tag, you can get it like: window.location.hash.
So window.location.href handles all in once... basically:
window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.hash === window.location.href;
//true
Also using window is not needed if already in window scope...
So, in that case, you can use:
location.protocol
location.hostname
location.pathname
location.hash
location.href
To get the path, you can use:
console.log('document.location', document.location.href);
console.log('location.pathname', window.location.pathname); // Returns path only
console.log('location.href', window.location.href); // Returns full URL
Open Developer Tools, type in the following in the console and press Enter.
window.location
Ex: Below is the screenshot of the result on the current page.
Grab what you need from here. :)
Use: window.location.href.
As noted above, document.URL doesn't update when updating window.location. See MDN.
Use window.location.href to get the complete URL.
Use window.location.pathname to get URL leaving the host.
You can get the current URL location with a hash tag by using:
JavaScript:
// Using href
var URL = window.location.href;
// Using path
var URL = window.location.pathname;
jQuery:
$(location).attr('href');
For complete URL with query strings:
document.location.toString()
For host URL:
window.location
// http://127.0.0.1:8000/projects/page/2?name=jake&age=34
let url = new URL(window.location.href);
/*
hash: ""
host: "127.0.0.1:8000"
hostname: "127.0.0.1"
href: "http://127.0.0.1:8000/projects/page/2?username=jake&age=34"
origin: "http://127.0.0.1:8000"
password: ""
pathname: "/projects/page/2"
port: "8000"
protocol: "http:"
search: "?name=jake&age=34"
username: ""
*/
url.searchParams.get('name')
// jake
url.searchParams.get('age')
// 34
url.searchParams.get('gender')
// null
To get the path, you can use:
http://www.example.com:8082/index.php#tab2?foo=789
Property Result
------------------------------------------
window.location.host www.example.com:8082
window.location.hostname www.example.com
window.location.port 8082
window.location.protocol http:
window.location.pathname index.php
window.location.href http://www.example.com:8082/index.php#tab2
window.location.hash #tab2
window.location.search ?foo=789
window.location.origin https://example.com
var currentPageUrlIs = "";
if (typeof this.href != "undefined") {
currentPageUrlIs = this.href.toString().toLowerCase();
}else{
currentPageUrlIs = document.location.toString().toLowerCase();
}
The above code can also help someone
Adding result for quick reference
window.location;
Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
ancestorOrigins: DOMStringList,
origin: "https://stackoverflow.com",
replace: ƒ, assign: ƒ, …}
document.location
Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
ancestorOrigins: DOMStringList,
origin: "https://stackoverflow.com",
replace: ƒ, assign: ƒ
, …}
window.location.pathname
"/questions/1034621/get-the-current-url-with-javascript"
window.location.href
"https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript"
location.hostname
"stackoverflow.com"
For those who want an actual URL object, potentially for a utility which takes URLs as an argument:
const url = new URL(window.location.href)
https://developer.mozilla.org/en-US/docs/Web/API/URL
Nikhil Agrawal's answer is great, just adding a little example here you can do in the console to see the different components in action:
If you want the base URL without path or query parameter (for example to do AJAX requests against to work on both development/staging AND production servers), window.location.origin is best as it keeps the protocol as well as optional port (in Django development, you sometimes have a non-standard port which breaks it if you just use hostname etc.)
You have multiple ways to do this.
1:
location.href;
2:
document.URL;
3:
document.documentURI;
Use this:
var url = window.location.href;
console.log(url);
In jstl we can access the current URL path using pageContext.request.contextPath. If you want to do an Ajax call, use the following URL.
url = "${pageContext.request.contextPath}" + "/controller/path"
Example: For the page http://stackoverflow.com/posts/36577223 this will give http://stackoverflow.com/controller/path.
The way to get the current location object is window.location.
Compare this to document.location, which originally only returned the current URL as a string. Probably to avoid confusion, document.location was replaced with document.URL.
And, all modern browsers map document.location to window.location.
In reality, for cross-browser safety, you should use window.location rather than document.location.
location.origin+location.pathname+location.search+location.hash;
and
location.href
does the same.
You can get the full link of the current page through location.href
and to get the link of the current controller, use:
location.href.substring(0, location.href.lastIndexOf('/'));
Short
location+''
let url = location+'';
console.log(url);
Getting the current URL with JavaScript :
window.location.toString();
window.location.href
if you are referring to a specific link that has an id this code can help you.
$(".disapprove").click(function(){
var id = $(this).attr("id");
$.ajax({
url: "<?php echo base_url('index.php/sample/page/"+id+"')?>",
type: "post",
success:function()
{
alert("The Request has been Disapproved");
window.location.replace("http://localhost/sample/page/"+id+"");
}
});
});
I am using ajax here to submit an id and redirect the page using window.location.replace. just add an attribute id="" as stated.
let url = new URL(window.location.href);
console.log(url.href);
Use the above code to get the current URL of the website.
or try this - https://bbbootstrap.com/code/get-current-url-javascript-54628697
Firstly check for page is loaded completely in
browser,window.location.toString();
window.location.href
then call a function which takes url, URL variable and prints on console,
$(window).load(function(){
var url = window.location.href.toString();
var URL = document.URL;
var wayThreeUsingJQuery = $(location).attr('href');
console.log(url);
console.log(URL);
console.log(wayThreeUsingJQuery );
});
Related
Redirect to different URL when current URL contains some string
I have a requirement wherein I need to redirect my page to a different URL when my current URL contains some string. For instance, If my current URL contains www.testdomain.com or www.testdomain.com/web/region then it should redirect to www.testdomain.com/group/region. I tried the below code but it returns "The requested resource could not be found -- https://www.testdomain.com/web/region/testdomain.com/group/region". $(document).ready(function() { if (window.location.href.indexOf("web/region") > -1) { window.location.href=window.location.hostname+'/group/region'; } }) It is adding the URL twice here. But when I pass the direct URL window. location.href="www.testdomain.com/group/region" it is working. Can someone guide me on how do I force redirect my page if the URL contains www.testdomain.com or www.testdomain.com/web/region? Thanks
Start with // so that the browser knows it's not a relative URI: window.location.href = '//' + window.location.hostname+'/group/region'; You can also prepend the protocol: window.location.href = window.location.protocol + '//' + window.location.hostname+'/group/region';
It's adding the URL twice because browsers interpret /group/region as a relative path, automatically prepending the current domain UNLESS otherwise specified. (Maybe others can explain why window.location.hostname doesn't immediately return, thus preventing the browser from assuming a relative path?) Example: If you explicitly set a domain, the browser will redirect to it as expected. window.location.href='http://www.google.com/' Otherwise, if you take away "www." window.location.href='google.com/' Your browser will redirect to "www.testdomain.com/google.com", appending the string. The fix is simple. Just delete window.location.hostname+ and it will only return the URL once. Or... for a better user experience, I would suggest using window.location.replace() which DOES NOT save the current page in session history.(You don't want to go back, just to be redirected again!) SOLUTION: Replace your return block with this. window.location.replace('/group/region')
Extract parameter from url path in javascript
I want to extract a part of an url path but i don't know how to do that. I want to get a value (3252) from a path like this: /forums/0-3252-1-1-my-topic-title.htm How can i do that ? Thank you
If you want to work with URL there is a powerful tool on which you can reply and that is JavaScript Window Location. The window.location object can be used to get the current page address (URL). The window.location object have many properties or method. Some examples: window.location.href returns the href (URL) of the current page window.location.hostname returns the domain name of the web host window.location.pathname returns the path and filename of the current page window.location.protocol returns the web protocol used (http: or https:) window.location.assign() loads a new document Example: Display the href (URL) of the current page: <script> alert(window.location.href); </script> Follow link to read more: Window Location
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;
What can I do to ignore the domain on a redirect request?
I have 3 domains, one is for development, another for QA and another for production. Let's say: dev-domain.com qa-domain.com prod-domain.com And I want to send the user to another location after he submits an email: window.location.href = 'confirmation.html'; So, what I want to avoid doing on every environment: window.location.href = 'dev-domain.com/confirmation.html'; or window.location.href = 'qa-domain.com/confirmation.html';, and only doing something like: window.location.href = '(Test-Domain)/confirmation.html'; Any suggestions?
file_name.html should be sufficient because it's relative path. Do you need to implement absolute paths?
window.location has several properties, like host, protocol, port, etc - https://developer.mozilla.org/en-US/docs/Web/API/Location You can change just the path of the Location object: window.location.pathname = '/confirmation.html';
Yes, as Oluwafemi Sule pointed out the window.location.hostname property returns the name of the internet host (of the current page). So you can use: window.location.href = window.location.hostname + '/confirmation.html';
Get The Current Domain Name With Javascript (Not the path, etc.)
I plan on buying two domain names for the same site. Depending on which domain is used I plan on providing slightly different data on the page. Is there a way for me to detect the actual domain name that the page is loading from so that I know what to change my content to? I've looked around for stuff like this but most of it doesn't work the way I want it to. For instance when using document.write(document.location) on JSFiddle it returns http://fiddle.jshell.net/_display/ i.e. the actual path or whatever that is.
How about: window.location.hostname The location object actually has a number of attributes referring to different parts of the URL
Let's suppose you have this url path: http://localhost:4200/landing?query=1#2 So, you can serve yourself by the location values, as follow: window.location.hash: "#2" window.location.host: "localhost:4200" window.location.hostname: "localhost" window.location.href: "http://localhost:4200/landing?query=1#2" window.location.origin: "http://localhost:4200" window.location.pathname: "/landing" window.location.port: "4200" window.location.protocol: "http:" window.location.search: "?query=1" Now we can conclude you're looking for: window.location.hostname
If you are not interested in the host name (for example www.beta.example.com) but in the domain name (for example example.com), this works for valid host names: function getDomainName(hostName) { return hostName.substring(hostName.lastIndexOf(".", hostName.lastIndexOf(".") - 1) + 1); }
function getDomain(url, subdomain) { subdomain = subdomain || false; url = url.replace(/(https?:\/\/)?(www.)?/i, ''); if (!subdomain) { url = url.split('.'); url = url.slice(url.length - 2).join('.'); } if (url.indexOf('/') !== -1) { return url.split('/')[0]; } return url; } Examples getDomain('http://www.example.com'); // example.com getDomain('www.example.com'); // example.com getDomain('http://blog.example.com', true); // blog.example.com getDomain(location.href); // .. Previous version was getting full domain (including subdomain). Now it determines the right domain depending on preference. So that when a 2nd argument is provided as true it will include the subdomain, otherwise it returns only the 'main domain'
If you wish a full domain origin, you can use this: document.location.origin And if you wish to get only the domain, use can you just this: document.location.hostname But you have other options, take a look at the properties in: document.location
You can get it from location object in Javascript easily: For example URL of this page is: http://www.stackoverflow.com/questions/11401897/get-the-current-domain-name-with-javascript-not-the-path-etc Then we can get the exact domain with following properties of location object: location.host = "www.stackoverflow.com" location.protocol= "http:" you can make the full domain with: location.protocol + "//" + location.host Which in this example returns http://www.stackoverflow.com I addition of this we can get full URL and also the path with other properties of location object: location.href= "http://www.stackoverflow.com/questions/11401897/get-the-current-domain-name-with-javascript-not-the-path-etc" location.pathname= "questions/11401897/get-the-current-domain-name-with-javascript-not-the-path-etc"
window.location.hostname is a good start. But it includes sub-domains, which you probably want to remove. E.g. if the hostname is www.example.com, you probably want just the example.com bit. There are, as ever, corner cases that make this fiddly, e.g. bbc.co.uk. The following regex works well for me: let hostname = window.location.hostname; // remove any subdomains, e.g. www.example.com -> example.com let domain = hostname.match(/^(?:.*?\.)?([a-zA-Z0-9\-_]{3,}\.(?:\w{2,8}|\w{2,4}\.\w{2,4}))$/)[1]; console.log("domain: ", domain);
Since this question asks for domain name, not host name, a correct answer should be window.location.hostname.split('.').slice(-2).join('.') This works for host names like www.example.com too.
If you are only interested in the domain name and want to ignore the subdomain then you need to parse it out of host and hostname. The following code does this: var firstDot = window.location.hostname.indexOf('.'); var tld = ".net"; var isSubdomain = firstDot < window.location.hostname.indexOf(tld); var domain; if (isSubdomain) { domain = window.location.hostname.substring(firstDot == -1 ? 0 : firstDot + 1); } else { domain = window.location.hostname; } http://jsfiddle.net/5U366/4/
Use document.write(document.location.hostname) window.location has a bunch of properties. See here for a list of them.
I figure it ought to be as simple as this: url.split("/")[2]
If you want to get domain name in JavaScript, just use the following code: var domain_name = document.location.hostname; alert(domain_name); If you need to web page URL path so you can access web URL path use this example: var url = document.URL; alert(url);
What about this function? window.location.hostname.match(/\w*\.\w*$/gi)[0] This will match only the domain name regardless if its a subdomain or a main domain
for my case the best match is window.location.origin
Combining a few answers from the above, the following works really well for me for destroying Cookies: /** * Utility method to obtain the domain URI: */ fetchDomainURI() { if (window.location.port.length > 0) { return window.location.hostname; } return `.${window.location.hostname.match(/\w*\.\w*$/gi)[0]}`; } Works for IP addresses with ports, e.g., 0.0.0.0:8000 etc, as well as complex domains like app.staging.example.com returning .example.com => allows for cross-domain Cookie setting and destroying.
I'm new to JavaScript, but cant you just use: document.domain ? Example: <p id="ourdomain"></p> <script> var domainstring = document.domain; document.getElementById("ourdomain").innerHTML = (domainstring); </script> Output: domain.com or www.domain.com Depending on what you use on your website.
Even if the question is about the domain name, the accepted solution includes the subdomain (eg. you get blog.example.com calling location.hostname). For future reference I suggest a one-liner to extract only the domain (eg. https://blog.example.com/index.html -> example.com) as Micheal. location.hostname.split('.').filter(( _, i) => i < 2).join('.') Beware! It can break when the TLD is composed of two parts (eg. .co.uk). If that's your case change 2 with 3 in the code above.
you can use this to do away with the port number. var hostname = window.location.host; var urlWithoutPort = `https://${hostname}`; console.log(urlWithoutPort);
https://publicsuffix.org/list/ (https://github.com/publicsuffix/list/blob/master/public_suffix_list.dat) is needed to correctly parse out all domains without suffixes, working with dots as in the answers above will never completely be correct. Feel free to run the above codes samples against the public suffixes dat file to realize this. You can roll your own code based on this or use a package like https://www.npmjs.com/package/tldts getDomainWithoutSuffix('google.com'); // returns `google` getDomainWithoutSuffix('fr.google.com'); // returns `google` getDomainWithoutSuffix('fr.google.google'); // returns `google` getDomainWithoutSuffix('foo.google.co.uk'); // returns `google` getDomainWithoutSuffix('t.co'); // returns `t` getDomainWithoutSuffix('fr.t.co'); // returns `t` getDomainWithoutSuffix('https://user:password#example.co.uk:8080/some/path?and&query#hash'); // returns `example`