Get Absolute Uri in JavaScript - javascript

How can i do the same in clean JavaScript
var url = '<%=Request.Url.AbsoluteUri%>';
Thats all!

The location.href property will contain that information.
var url = location.href;

Related

How to fetch appended part of url using javascript

How can we get part of string added to the url using java script.
example: my url link is in below format:
https://domain/imp/s/testrecordname/idoftestrecord/selectedrecord?languagelocale
i tried below ways.
var v1=window.location.href
var v2=window.location.host
var v3=window.location.hostname
var v4=window.location.protocol
var v5=window.location.pathname
var v6=window.location.search
var v7=window.location.hash
but i am unable to get "testrecordname/idoftestrecord/selectedrecord" this portion of url.
can anyone suggest how to attain it
You need to manipulate the pathname
const url = new URL("https://domain/imp/s/testrecordname/idoftestrecord/selectedrecord?languagelocale")
console.log(url.href);
console.log(url.host);
console.log(url.hostname);
console.log(url.protocol);
console.log(url.pathname); // this???
console.log(url.pathname.split("/s/")[1]); // for example
console.log(url.search);
console.log(url.hash);
One way could be extracting from URL string using string functions
let s = 'https://domain/imp/s/testrecordname/idoftestrecord/selectedrecord?languagelocale';
console.log(s.slice(s.indexOf('s/')+2,s.indexOf('?')));

Can I safely use domain in URI

I am getting the hostname using javascript:
var url = window.location.hostname;
Can I then safely (and correctly) use this as part of a url, or should I do something to it first?
var newUrl = 'http://domain.com/file.php?url='+url;
In the php file I will get said url using $_GET (and verify validity of domain name)
No, you need to encode it so that you can use it in a url:
var newUrl = 'http://domain.com/file.php?url=' + encodeURIComponent(url);
Note that you don't have to change anything on the server-side, $_GET['url'] is already decoded.

how to get the full path of a page in javascript

I know that var pathname = window.location.pathname; returns path only and var url = window.location.href; returns full URL.
Now suppose i have page MyPageName.aspx in the root of my site and my site can be deployed on servers serverone, servertwo & serverthree.
On Serverone, i want to display http://example.com/MyPageName.aspx
On servertwo, i want to display http://example.net/MyPageName.aspx
On serverthree, i want to display http://example.org/MyPageName.aspx
So how do i get the full URL path of a page in i'ts current environment with out browising to that page but knowing in advance that the page exists.
I want to display the URL some where on a master page.
You can use window.location.origin to return everything up to and including the .com:
var url = window.location.origin;
-> "http://example.com"
As MyPageName.aspx appears to be static, you can then just use string concatenation to append it to the end:
url += "/MyPageName.aspx";
-> "http://example.com/MyPageName.aspx"
Demo
var url = window.location.origin + "/MyPageName.aspx";
document.write(url);
Did you try
document.URL
read http://www.w3schools.com/jsref/prop_doc_url.asp

Undefined pathname in js

I am trying to extract the base of the url:
http://google.com/something --> http://google.com
http://127.0.0.1:8000/something --> http://127.0.0.1:8000
When I try and use the following:
var pathArray = window.location.pathname;
alert(pathArray);
I get pathArray = undefined. Note: using location.hostname does work here. Why is .pathname returning undefined, and how would I get the url base here? Thank you.
Update: I used var pathArray = 'http://' + window.location.
https://developer.mozilla.org/en-US/docs/Web/API/Window/location
May not work in all browsers, as per usual
I'd just get window.location and parse it.
Try window.location.host, it works for http://localhost:8000, so it should work for your case

Get relative path of the page url using javascript

In javascript, how can I get the relative path of the current url?
for example http://www.example.com/test/this?page=2
I want just the /test/this?page=2
Try
window.location.pathname+window.location.search
location.href
holds the url of the page your script is running in.
The quickest, most complete way:
location.href.replace(/(.+\w\/)(.+)/,"/$2");
location.href.replace(location.origin,'');
Only weird case:
http://foo.com/ >> "/"
You can use the below snippet to get the absolute url of any page.
var getAbsoluteUrl = (function() {
var a;
return function(url) {
if(!a) a = document.createElement('a');
a.href = url;
return a.href;
}
})();
// Sample Result based on the input.
getAbsoluteUrl('/'); //Returns http://stackoverflow.com/
Checkout get absolute URL using Javascript for more details and multiple ways to achieve the same functionality.
I use this:
var absURL = document.URL;
alert(absURL);
Reference: http://www.w3schools.com/jsref/prop_doc_url.asp
You should use it the javascript way, to retrieve the complete path including the extensions from the page,
$(location).attr('href');
So, a path like this, can be retrieved too.
www.google.com/results#tab=2

Categories

Resources