I want to get the url with javascript. But I just need http://www.mysite.com, without any parameters behind it. any idea how to get that?
Use this:
var url = window.location.protocol + "//" + window.location.host;
window.location.hostname
try
window.location.protocol+'//'+window.location.hostname
Related
I am new to javascript, and stuck on this problem. I am trying to have the url query string change based on which checkbox checked. But I seem to have some issue with window.location, Some of the JavaScript is below
var urltest= window.location.pathname;
var urltest2 = window.location.host;
var currentURL = urltest2 + urltest;
url = currentURL + "?" + arr_opts.join('&');
document.getElementById('myurl').innerHTML = url;
//window.location.href = url;
window.location = url;
The window.location does not work here, but when I change var currentURL to
var currentURLL = window.location.href;
it is work, but not with
var urltest= window.location.pathname;
var urltest2 = window.location.host;
var currentURL = urltest2 + urltest;
I need window.location to point page to the currentURL above.
Any help would be appreciated.
You're not providing a protocol, so the location.href change is being treated as "go to this relative path from the current location", i.e. on this page
window.location = window.location.host + window.location.pathname;
// takes us to
// http://stackoverflow.com/questions/35254564/javascript-window-location-not-working/stackoverflow.com/questions/35254564/javascript-window-location-not-working/35254601
Do one of the following
Provide a protocol so it knows it is an absolute URI,
window.location = window.location.prototcol + '//' + window.location.host + window.location.pathname + '?foo=bar';
Tell it to re-use the current protocol but work as an absolute URI
window.location = '//' + window.location.host + window.location.pathname + '?foo=bar';
Provide an origin (instead of host)
window.location = window.location.origin + window.location.pathname + '?foo=bar';
Tell it to re-use the same origin but work as an absolute path
window.location = window.location.pathname + '?foo=bar';
Just update the query
window.location = '?foo=bar';
Always choose the most simple option to make your life easier if you ever need to debug, i.e. if you can assume you will always want the same protocol, host and path, just update the query.
Useful knowledge
Starting a URL with..
// means same protocol
/ means same origin
? means same path
# means same query (will not re-load)
The pathname is going to be something like /questions/35254564/javascript-window-location-not-working, and the host is something like stackoverflow.com. If you put those together with code like yours, you get
stackoverflow.com/questions/35254564/javascript-window-location-not-working
That's clearly not correct; the host looks like part of the pathname. You can use protocol and port if you really feel the need to reconstruct the URL from its constituent parts, but using the href seems simpler if all you want to do is add a query string.
What I want is do this:
var siteBaseUrl = window.location.origin;
But in IE 9 it's returning undefined
Trying to understand how do I use modernizr from the suggestion here:
$window.location.origin gives wrong value when using IE
I updated my code to add this block before my siteurl:
if (!window.location.origin) {
window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
}
var siteBaseUrl = window.location.origin;
So now the above works, but I am not sure if modernizr suggestion in the link above can help me do it differently.
Or may be I am confusing myself and the above code is working due to modernizr.
All modernizr will do in this case is tell you that the method is missing. Since it does not fix/polyfill anything itself, you would have to do the same work.
There would be no difference.
I am using
history.pushState('id','myTitle','myURL');
to manipulate the displayed URL and the history stack.
At some point I am pushing get parameters like so:
history.pushState('id','myTitle','?mySubLinkedStuff=hotSubLinkedStuff');
Now when I do
history.pushState('id','myTitle','#justSomeHashtag');
it produces http://example.com?mySubLinkedStuff=hotSubLinkedStuff#justSomeHashtag
I can also overwrite the value of mySubLinkedStuff but not seem to be able to get rif of it alltogether.
Desired result:
http://example.com#justSomeHashtag or http://example.com/#justSomeHashtag
and obviously I don't want to make the whole roundtrip over the server and I also want to avoid using absolute path or URL to keep the project portable.
As NimrodArgov rightly remarked: overwriting existing get-parameter strings works if you
push the full URL.
So for ensuring portability of the app (keep it usable on various domains) I did this:
history.statePush(document.location.origin + window.location.pathname + '#myHashValue');
Works perfectly well and fast.
To push current address without GET parameters :
history.pushState("id", "myTitle",
location.protocol + "//" + location.host +
location.pathname + location.hash
);
Other than that ;
To push a hash change I could do :
history.pushState("id", "myTitle",
location.protocol + "//" + location.host +
location.pathname + location.search + "#myHashHere"
);
To push a query change I could do :
history.pushState("id", "myTitle",
location.protocol + "//" + location.host +
location.pathname + "?my=query&over=here" + location.hash
);
※ Sorry I don’t have enough karma to just comment on Max’s answer… :/
I'm making an ebay template for myself, and I want to use a name anchor to jump to different section on the page template. But Ebay adds something to the URL therefore breaking the name anchor.
Since this seems to be Firefox related only, someone suggested that I need to strip "&bv=mozilla" from the URL then it would work. If there are any javascript experts out here that can help me out, I would highly appreciate it.
var documentUrl = location.href;
var newUrl = documentUrl.replace("&bv=mozilla","");
Try this:
var url = location.protocol + '//' + location.host + location.pathname;
or this:
var url = location.protocol + '//' + location.host + ':' + location.port + location.pathname;
if it is on the same page.
Otherwise, you could try this:
var url = 'insert URL here';
var new_url = url.substring(0, url.indexOf('?'));
See substring, indexOf and window.location.
I'm looking for a neat way of getting the URL of the current document in Javascript.
The URL should be clean of parameters (?parameter1=bla¶meter2=bla)
The URL should be clean of hash tags (#jumppoint)
http/https should be removed/consolidated into http
I know i can get the current URL with location.href and then use some regular expressions to clean it up but maybe there is a nicer/cleaner solution for getting rid of the junk?
There are many other parameters than the href in window.location. See full reference here: https://developer.mozilla.org/en/DOM/window.location
What you are looking for as a starter might be the window.location.hostname:
"the host name (without the port number or square
brackets)."
From the example URL http://[www.example.com]:80/search?q=devmo#test the hostname will be www.example.com.
If you also want to include the path and force a http:// protocol, try:
'http://' + window.location.hostname + window.location.pathname;
As a side note, a nifty trick to get the same parameters from another URL than the window.location is to create an empty anchor:
var a = document.createElement('a');
a.href = 'http://www.example.com:80/search?q=devmo#test';
console.log('http://' + a.hostname + a.pathname);
None of the given answers address the fact that the protocol can be http or https as in the OPs title. To accommodate this I suggest:
document.location.protocol +"//"+ document.location.hostname + document.location.pathname
The Location object got what you need
window.location.hostname + window.location.pathname
You have document.location object, so:
var oLoc = document.location,
sUrl = oLoc.protocol + oLoc.hostname;
// or "http://" + oLoc.hostname
You can use these replacement functions to remove the hash and search arguments and normalize https to http:
url = url.replace(/#[^#]*$/, "").replace(/\?[^\?]*$/, "").replace(/^https:/, "http:");
Or, if all you really want is the domain and path, you can just use this:
window.location.hostname + window.location.pathname
Please try this snippet:
if (!window.location.origin){
// For IE
window.location.origin = window.location.protocol + "//" + (window.location.port ? ':' + window.location.port : '');
}
url = window.location.origin + window.location.pathname;
document.write('Origin url: ' + url);
This page indicates that you could probably use window.location.host to get the part you're actually interested in. I haven't tested it, though.
Try:
window.location.hostname;