I have constructed a url path that are pointing to different hostname www.mysite.com
, so for example:
var myMainSite = 'www.mymainsite.com' + '/somepath';
so this is equivalent to www.mymainsite.com/path/path/needthispath/somepath
.
How I'm doing it now is like the code below and this gives me a bunch of indexes of the url in the console.log
.
var splitUrl = myMainSite.split['/'];
console.log
looks like:
0: //
1: www.
2: mysite.com
3: path
4: path
5: needthispath
6: somepath
and I concat them like splitUrl[5]+'/'+splitUrl[6]
and it doesn't look pretty at all.
So my question is how to split/remove url location //www.mymainsite.com/
to get the url path needthispath/somepath
in js?
Is there a quicker and cleaner way of doing this?
I want to SPLIT some specific parts of the URL, here is what i have so far.
Nội dung chính
- JavaScript Basic: Exercise-144 with Solution
- JavaScript: Tips of the Day
- How do you separate a URL?
- How to extract url in Js?
- How to get specific part of url in JavaScript?
- How do you split a URL in HTML?
var query = window.location.pathname.split[ '/' ];
query = window.location.pathname.split[ '.html' ];
var redirectpath = "//www.mydomain.com/search/?q="
window.location.href = redirectpath + query;
The URL structure will be like this:
//www.mydomain.com/page/2013/05/some-page-title.html
The variable query
outputs like this; page,2013,05,some-page-title
i only want the some-page-title
part and also remove the hyphens.
so the final output would be //www.mydomain.com/search/?q=some page title
how is that possible? Please help!! Thanks
asked Aug 15, 2013 at 14:20
1
Split returns an array, use it as an array!
var parts = window.location.pathname.split[ '/' ];
var query = parts[parts.length-1].split[ '.html' ];
query[0]= query[0].replace[/-/g," "];
var redirectpath = "//www.mydomain.com/search/?q="
window.location.href = redirectpath + query[0];
This assuming you always want the part of the url after the last /
Alejandra
7043 gold badges9 silver badges25 bronze badges
answered Aug 15, 2013 at 14:23
2
//get the url
var url = window.location;
//function to get the hostname and pathname
//var l = getLocation[url];
//split the pathname by /
var array = url.pathname.split['/'];
//fix the url protocol and hostname for concate
var pathnames = window.location.protocol + "//" + window.location.host;
//loop to get the splited url pathname and insert the each url in specific div
for [i = 1; i < array.length; i++] {
//concatenate the path for each loop
pathnames = pathnames + '/' + array[i];
//appending an ancher tag with href for path in the element with id table_panel_header
$["div#table_panel_header"].append[
''
+ array[i] + '/'];
}
//example text seen as: Complaint_Module/master/complaint-items/
/* example href will append like
Complaint_Module/
master/
complaint-items/
*/
answered Sep 24, 2015 at 8:48
JavaScript Basic: Exercise-144 with Solution
Write a JavaScript program to break an address of an url and put it's part into an array.
Note: url structure : ://.org[/] and there may be no part in the address.
Pictorial Presentation:
Sample Solution:
HTML Code:
Break an address of an url and put it's part into an array.
JavaScript Code:
function break_address[url_add] {
var data = url_add.split["://"]
var protocol = data[0];
data = data[1].split[".com"];
var domain = data[0];
data = data[1].split["/"];
if[data[1]]{
return [protocol,domain,data[1]]
}
return [protocol,domain]
}
var url_add = "//www.w3resource.com/javascript-exercises/"
console.log["Original address: "+url_add]
console.log[break_address[url_add]]
Sample Output:
Original address: //www.w3resource.com/javascript-exercises/ ["https","www.w3resource","javascript-exercises"]
Flowchart:
ES6 Version:
function break_address[url_add] {
let data = url_add.split["://"];
const protocol = data[0];
data = data[1].split[".com"];
const domain = data[0];
data = data[1].split["/"];
if[data[1]]{
return [protocol,domain,data[1]]
}
return [protocol,domain]
}
var url_add = "//www.w3resource.com/javascript-exercises/"
console.log[`Original address: ${url_add}`]
console.log[break_address[url_add]]
Live Demo:
See the Pen javascript-basic-exercise-144 by w3resource [@w3resource] on CodePen.
Improve this sample solution and post your code through Disqus
Previous:
Write a JavaScript program to sort the strings of a given array of strings in the order of increasing lengths.
Next: Write a JavaScript program to find the maximum integer n such that 1 + 2 + ... + n