Get last segment of url without parameters in javascript? - javascript

Ho do I get the last part of a URL segment excluding any URL parameters.
So if I'm on this page:
http://example.com/myfolder/mypage.aspx?x=1
I want to get:
mypage.aspx

You can do something this way:
// The first thing is to strip off the things after query string:
var url = "http://example.com/myfolder/mypage.aspx?x=1";
url = url.split("?")
url = url[0];
// Get the last path:
url = url.split("/");
page = url[url.length-1];
alert(page);
The above path will work even if there's no ? in it.

Related

Adding parameters to url on page load only if no paremeters already exist

I searched for the answer to my question and even tried some solutions, but wasn't able to get anything to really work. I'm newish to javascript, so that might also be why.
I have a specific URL and whenever someone goes to that URL, I want to add parameters to it, but only if no parameters are already present. Parameters get added to the URL for other on click events, but on page load, I need a set of parameters added to the URL.
I tried to use the history API and I think I'm kind of close, but I'm not able to get it to do what I want it to do.
function addDefaultParam(url) {
var currentURL = window.location.href; //get the current url
var baseURL = '/our-partners'; //this is the url that should have params added
var paramString = '?asc=true&sortBy=display_name'; //here are the params
if (currentURL === baseURL) {
window.history.pushState("object or string", "Title", "/" + paramString);
}
return url;
}
I'm using basic js in this because that's what was used in the other functions (I inherited this code). Any help would be greatly appreciated.
You can register the addDefaultParam function to fire when the document first loads in the browser and use the Location interface to check the state of the current path and query string of the URL and if they match your conditions, update the current query string value.
See below for an example:
window.addEventListener("load", addDefaultParam);
function addDefaultParam() {
let currentPath = document.location.pathname;
let currentQueryString = document.location.search;
let targetPath = "/our-partners";
if (currentPath === targetPath && !currentQueryString) {
document.location.search = "?asc=true&sortBy=display_name";
}
}

How to deal with a friendly url with parameters in a static webpage?

I have the following URL https://mywebsite/somepage/1234/5678 where somepage is a route to a page and the numbers are are parameters.
My page is a static page, just html and javascript. Ex.: https://mywebsite/somepage.html.
How could I open this page given the above url get the parameters inside the page?
The reason for this is that I have a mobile deeplink direct the user to a website so that it can download the app in case it isn't installed or to the app itself. I don't have the choice to use a dinamic page with a routing system like in Cake PHP or in Spring Framework.
If you want to load the normal page (https://mywebsite/somepage.html) then you could use hashes instead. They don't get seen by the server, so it would serve the regular html file.
// get the url
var url = window.location.href; // = "https://mywebsite/somepage.html#1234/5678"
// get the bit you want
var params = url.substring(32); // = "1234/5678"
// ^ the length of "https://mywebsite/somepage.html#"
// split it apart into individual parts
params = params.split("/"); // = ["1234", "5678"]
// then do whatever you like with the params
Create array from current pathname split on /
var urlPath = window.location.pathname.split('/');
Do something with the array...
urlPath.forEach(function(item) {
console.log(item);
});

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

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;
}

Jquery extract string from url and load remote element

I'm new in Jquery, I would like to have Jquery code to get the current page url and if the url contains certain string then load remote element.
example:
i have the page urls like this:
"http://......./Country/AU/result-search-to-buy"
"http://......./Country/CA/result-search-to-buy"
"http://......./Country/UK/result-search-to-buy"
the part "/Country/AU" is what I need to determine which page element I should load in, then if "AU" I load from "/state-loader.html .state-AU", if "CA" I load from "/state-loader.html .state-CA"
I have a builtin module "{module_pageaddress}" to get the value of the current page url, I just dont know the Jquery logic to let it work.
I expect something like this:
if {module_pageaddress} contains "/Country/AU/"
$('#MyDiv').load('state-loader.html .state-AU');
if {module_pageaddress} contains "/Country/CA/"
$('#MyDiv').load('state-loader.html .state-CA');
You can try the following if the country string is always last before the last slash in the URL
See a live fiddle
var url = "http://......./Country/AU/result-search-to-buy"
var loc = url.split( '/' );
//should be using the next line in live
//var loc = window.location.pathname.split( '/' );
var country = alert(loc [loc.length-2]);

Categories

Resources