I want to get url without page. Please see it below.
current url
http://localhost/FolderPath/Page.html
want to get
http://localhost/FolderPath/
How can I do this? Thanks.
var oldPath = 'http://localhost/FolderPath/Page.html';
var newPath = oldPath.split('/').slice(0, -1).join('/') + '/';
"http://localhost/FolderPath/Page.html".replace(/[^\/]*$/, '');
var url = "http://localhost/FolderPath/Page.html";
url = url.substr(0, url.lastIndexOf('/') + 1);
Regex anyone?
var url = "http://localhost/FolderPath/Page.html";
url.match(/(.+)(\/(.+)?)$/)[1] + "/";
//returns "http://localhost/FolderPath/"
var url = "http://localhost/FolderPath/";
url.match(/(.+)(\/(.+)?)$/)[1] + "/";
//returns "http://localhost/FolderPath/"
Related
How can I remove some part of an url and add some query before returning it?
Example:
locahost:8080/product/orders/1.
I want to remove the orders/1 and add /?query="sample".
Use replace function:
location.replace("locahost:8080/product/?query='sample'")
You can get the url by simply doing window.location.href. You can then edit it by copying it to some new var newURL = window.location.href;
newUrl = newUrl.replace('/orders/1', '/?query=\"sample\"');
window.location.href = newUrl; // execute this to pass on the parameters to the current page.
Suppose you have url like this in variable1, let say like this
var variable1 = 'http://locahost:8080/product/orders/1'; //here you can get the actual browser url using `window.location.href`and assign it to `variable1`
just use replace function:
var final_text = variable1.replace('/orders/1','?query=sample');
you will get the following output, you do console.log(final_text);
http://locahost:8080/product?query=sample
You can try something like
var url = window.location.href;
var query = "somestring" ;
window.location.replace(url + "&" + somestring);
removing the / pieces:
var newloc = url.substring(0, url.search("/")) + query;
window.location.replace(newloc);
Sorry for asking simple questions.
I have url with like this :
http://sub.maindomain/page/title
I want to add
/en/
in the middle of my url so it will be change to
http://sub.maindomain/en/page/title
thank you for your help
var str='http://sub.maindomain/page/title';
var en='en/';
var position= str.indexOf('page')
alert(str.substr(0, position) + en + str.substr(position));
i guess there will be always page in that url so i found it's index,
and added en before it's index
If you want to change the actual URL with a new URL parameter en in the address bar. You can try this.
var newUrl = location.origin + '/en' + location.pathname
document.location.href = newUrl
Supposing you're in a context where document is available (since your post is taggued jQuery, it's most likely the case):
const href = 'http://sub.maindomain/page/title?foo=bar#hash';
// Build a link element
const link = document.createElement('a');
link.href = href;
// Add '/en' before its pathname
const result = link.protocol + '//' + link.host + '/en' + link.pathname + link.search + link.hash;
console.log(result);
in case if page is not on after domain some other string is there then try below code.
var str = 'http://sub.maindomain/page/title';
var arr = str.split(/\//);
arr.splice(3, 0, 'en');
str = arr.join('/');
let url = "http://sub.maindomain/page/title";
let textToAdd = "en/";
//Split the url string into array of two elements and then join them together with a new string 'en/page' as a separator
let newUrl = url.split('page').join(`${textToAdd}page`);
console.log(newUrl);
I hope to get the path of a filename, just like get the result /storage/emulated/0/Pictures/ when I input the string /storage/emulated/0/Pictures/Photo-2017-02-17-10-08-38_2271.JPG
How can I do in javascript? Thanks!
Try
var fullpath = "/storage/emulated/0/Pictures/Photo-2017-02-17-10-08-38_2271.JPG"
var pathWithoutFileName = fullpath.substr(0, fullpath.lastIndexOf('/') + 1);
var x = "/storage/emulated/0/Pictures/Photo-2017-02-17-10-08-38_2271.JPG"
var path = x.substr(0,x.lastIndexOf('/'));
var fullpath = "/storage/emulated/0/Pictures/Photo-2017-02-17-10-08-38_2271.JPG"
var path = fullpath.substr(0, fullpath.lastIndexOf('/') + 1);
The +1 is to include the last forward slash.
I am want add string in between the url
I have an url
hostname.com/test1/test
I want to add test2 before the test
it should be hostname.com/test1/test2/test
Do I need break the strings with / and then again build the string by adding tests?
Or is there any other way I can work on?
var url ="hostname.com/test1/test";
var lastSlash = url.lastIndexOf("/");
var output = url.substring(0,lastSlash) + "/test2" + url.substring(lastSlash, url.length);
console.log(output);
Hope this helps.
var url = 'hostname.com/test1/test';
var urlParts = url.split('/');
urlParts.splice(urlParts.length-1, 0, "test2");
alert(urlParts.join('/'));
I have a URL like this:
http://www.url/name/something/#/venue
I need to grab the 'venue' from each URL. I have:
var currentUrl = $(location).attr('href');
var urlParts = currentUrl.split( "/" );
The split breaks up the URL.
I'm not sure how to grab the last part of urlParts.
urlParts[urlParts.length - 1];
Just grab the last element of the urlParts array:
var lastPart = urlParts[urlParts.length - 1];
Instead of $(location) why not just use location object and location.hash? Like this:
location.hash.replace('#/', '')
For url like http://www.url/name/something/#/venue it will give you venue.
You can get it like this:
var url = document.URL.split('#/')[1];
var currentUrl = $(location).attr('href');
var urlParts = currentUrl.split( "/" );
alert(urlParts[urlParts.length-1]);
The fiddle here.
To grab the last element in the array urlParts, just use:
var last = urlParts[urlParts.length-1];
As an alternate way to do this, you could do:
var last = currentUrl.substr( currentUrl.lastIndexOf("/")+1 );