Window.location.href not works when url has # symbol in it - javascript

I have a popup call which takes the user credentials and login the user. Usully user would like to same page after login also. That's way I want to refresh the current page. I am using
window.location.href="currentrul";
This works just fine when we don't have # symbol in url. Let's say my URL is something like http://www.test.com/faq#shipping, it won't works. Any suggestions are appreciable.
Thanks in advance

If you want to refresh the page, use:
window.location.reload();

No, because # signifies an anchor on the same page.
The fragment identifier introduced by a hash mark # is the optional
last part of a URL for a document. It is typically used to identify a
portion of that document. The generic syntax is specified in RFC 3986.
The hash mark separator in URIs does not belong to the fragment
identifier.
Source: http://en.wikipedia.org/wiki/Fragment_identifier

try this:
var currentURL = window.location.href;
if(currentURL.indexOf('#') != -1){
currentURL = currentURL.substr(0, currentURL.indexOf('#'));
}
window.location.href = currentURL;

Related

Get HTML Current Page name alone, followed by ".htm" from URL or Pathname using Jquery & Javascript

I have Scenario like: Below,
Following a "Menu List" having href value set to corresponding ".htm" Pages inside "Menu.html"
Click Me to got to pageOne.htm
Click Me to got to pageTwo.htm
Click Me to got to pageThree.htm
Click Me to got to pageFour.htm
Each Pages pageOne.htm, pageTwo.htm & pageThree.htm etc.. are having footer part, In footer it must be like it has to contain 3 Links with href values.
I require a solution:
If my Current page is pageOne.htm the Footer should show me shortcuts links to pageTwo.htm, pageThree.htm and pageFour.htm and wise verse depending on my current page.
So I require code to Get .htm from URL pathname.
Can we find() method of Jquery after getting the URL PATH like find(".htm") from URL.
Require just a snippet of Code to get the .htm page name alone.
Including an sample case below:
What if the Url is Like >>
"http://<10.10.21.26:9080>/myTestAoo/pageThree.htm#detailView"
I wish to get the value "pageThree.htm" alone from above url, if its the current page.
Thanks in advance
var lastPartOfUrl = document.URL.split('/').pop();
Or with regular expression:
var regexp = /([^\/]+)(.htm)/;
var match = regexp.exec(document.URL);
console.log(match[0]); // pageThree.htm
console.log(match[1]); // pageThree
console.log(match[2]); // .htm
The following will give you the current URL, which you can tidy up using replace():
window.location.href
you may try this code:
var a = window.location.href;
var fileName = a.substring(a.lastIndexOf('/')+1);
alert(fileName);
use window.location.pathname; will returns the full path
try following function
function getFileName(){
var url = window.location.pathname;
var path=url.split("/");
var filname=path[path.length-1];
return filname;
}

How can I get everything after the forward slash in my url?

When I visit my url, my script will get the current URL and store it in a variable with
var currentURL = (document.URL);
I'd like to get everything after the forward slash in my url because there will be a hash ID after the forward slash like this:
www.mysite.com/XdAs2
so this is what would be stored in my variable currentURL and I'd like to take only the XdAs2 from it and store that into another variable. In addition, I'd like to know two other things.
Is document.URL the best way to get the current URL or will I have issues with some browsers?
If I were to try to open that URL using an iframe, will document.URL still work? or must there be an address bar present containing the url? I would really appreciate answers for those questions three questions. Thank you
Here's some pseudo code:
var currentURL = (document.URL); // returns http://myplace.com/abcd
var part = currentURL.split("/")[1];
alert(part); // alerts abcd
Basically:
1) document.URL should work fine in all major browsers. For more info refer to this Mozilla Developer Network article or this SO question
2) for an iframe, you need to write something like: document.getElementById("iframe_ID").src.toString()
Using jquery, you can do he following in order to access every inch of the current URL:
www.mysite.com/XdAs2?x=123
assuming you have the following url:
1- get the url in a jQuery object
var currentUrl = $(location)
2- access everything using the following syntax
var result = currentUrl.attr('YOUR_DESIRED_PROPERTY');
some common properties:
hostname => www.mysite.com
pathname => XdAs2
search => ?x=123
I hope this may help.
If you want everything after the host, use window.location.pathname
Following on from Mohammed's answer you can do the same thing in vanilla javascript:
var urlPath = location.pathname,
urlHost = location.hostname;

Removing # from fragment identifier

I try to remove the '#' from the url after removing the fragment identifier.
For example:
var myUrl = location.href; // example.com/#abc
alert(location.hash); // #abc
location.hash = '';
alert(myUrl); // example.com/#
Now i try to remove the # WITHOUT a page reload. I tried to use the replace function but when i try to remove the '#' the page will reload. Is there a solution to solve this ?
You should use HTML5 History API for this: http://diveintohtml5.info/history.html
You won't be using hashes anymore, but you can still load pages without reloading.
Look at this questions:
How to remove the hash from window.location with JavaScript without
page refresh?
Remove hash from url

JavaScript current URL check

I am wondering how I would get JavaScript to check if a user is on a certain URL so i can build an if statement from the result.
My reasoning is that if a user clicks on a link in the menu and they are currently on trucks.php the javascript will redirect them to a certain page. If they are not on trucks.php they will be directed to a different page.
Cheers guys.
The current location is in location.href.
The location object also contains some other useful fields:
location.hash: The part after the # in the URL
location.host: Hostname including port (if specified)
location.hostname: Just the hostname
location.pathname: The requested URI without protocol/host/port; starting with a /
location.port: The port - only if one is specified in the URL
location.protocol: Usually 'http:' or 'https:' - mind the colon at the end
In your case the most fail-safe way to check if the filename is trucks.php is this:
var parts = location.pathname.split('/');
if(parts[parts.length - 1] == 'trucks.php') {
location.href = 'some-other-page';
}
If you want to redirect without keeping the current page in history, use the following code instead of the location.href assignment:
location.replace('some-other-page');
Use window.location.href to get the current URL, or window.location.pathname to get just the path. For your specific problem, just the path name is required for the solution:
if (window.location.pathname == "/trucks.php")
window.location = "/somewhereelse.php";
Check out the MDC documentation for window.location.
Use window.location

Get the current url but without the http:// part bookmarklet!

Guys I have a question, hoping you can help me out with this one. I have a bookmarklet;
javascript:q=(document.location.href);void(open('http://other.example.com/search.php?search='+location.href,'_self ','resizable,location,menubar,toolbar,scrollbars,status'));
which takes URL of the current webpage and search for it in another website. When I use this bookmarklet it takes the whole URL including http:// and searches for it. But now I would like to change this bookmarklet so it will take only the www.example.com or just example.com (without http://) and search for this url. Is it possible to do this and can you please help me with this one?
Thank you!
JavaScript can access the current URL in parts. For this URL:
http://css-tricks.com/example/index.html
window.location.protocol = "http"
window.location.host = "css-tricks.com"
window.location.pathname = "/example/index.html"
please check: http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/
This should do it
location.href.replace(/https?:\/\//i, "")
Use document.location.host instead of document.location.href. That contains only the host name and not the full URL.
Use the URL api
A modern way to get a part of the URL can be to make a URL object from the url that you are given.
const { hostname } = new URL('https://www.some-site.com/test'); // www.some-site.com
You can of course just pass window location or any other url as an argument to the URL constructor.
Like this
const { hostname } = new URL(document.location.href);
Do you have control over website.com other.example.com? This should probably be done on the server side.
In which case:
preg_replace("/^https?:\/\/(.+)$/i","\\1", $url);
should work. Or, you could use str_replace(...), but be aware that that might strip 'http://' from somewhere inside the URL:
str_replace(array('http://','https://'), '', $url);
EDIT: or, if you just want the host name, you could try parse_url(...)?
Using javascript replace via regex matching:
javascript:q=(document.location.href.replace(/(https?|file):\/\//,''));void(open('http://website.com/search.php?search='+q,'_self ','resizable,location,menubar,toolbar,scrollbars,status'));
Replace (https?|file) with your choice, e.g. ftp, gopher, telnet etc.

Categories

Resources