How to get last part of URL, ignoring any GET parameters? - javascript

I'm expecting URLs in this for:
/user/username
but end users can also add whatever get parameters they want, like so:
/user/username?foo=bar
With that said, using AngularJS, what's the preferred way for me to get just the username (which appears after /user/) without anything else after it?

You should use the $location service and its .path() method, then use a regular split() and indexing.

I doubt there's a dedicated function for it but it seems easy enough to pull it out of the string with the query.
Get the path with $location.path() and then both of these does the job for you.
url.substring(6, (url.indexOf('?') != -1 ? url.indexOf('?') : url.length))
url.split('/')[2].split('?')[0]
Same question here: Is there a built-in way to get the current URL without any query parameters?

you can use window.location or $location service to get the path and then using split function
like this
var url = "/user/username?foo=bar";
var check = url.split('/user/');
var username = check[1].split('?');
console.log(username[0]);
you can find username by applying multiple split on your url. hope this is what you want.

Related

Getting query string from re written URL

I have url "SampleProject/profile/aA12". How can I get the value of the id from my rewritten URL using javascript? I want to get the "aA12" value.
Im using htaccess rewrite to rewrite my URL. Im new in rewritting url's. Any help will be appreciated. More powers and thank you.
You can use regex.
Try
'SampleProject/profile/aA12'.match(/\SampleProject\/profile\/(\w+)/)
'SampleProject/profile/aA12/xxx'.match(/\SampleProject\/profile\/(\w+)/)
'aA12' will be matched in both cases.
There are going to be quite a few ways to achieve your goal with JavaScript. A simple solution could be something like this:
let myURL = "SampleProject/profile/aA12";
let result = myURL.split('/').pop();
// returns "aA12"
The .split('/') method is dividing your string up into an array using the / character, and .pop() is simply returning the last element of that array.
Hope this helps! If you were looking for more advanced matching, i.e. if you wanted to ignore a potential query string on the end of the URL parameter, you could use regular expressions.
Their is a many way that you can use to achieve the desired method i made you a code pen in this link
var url = "SampleProject/profile/aA12";
let res = url.split('/').pop();
console.log(res)
https://codepen.io/anon/pen/KQxNja

Use URL Parameter to call a line of text using JS?

I'm hoping to call a line of text by using a simple URL parameter. Say I had an ordered list in javascript and on load of url example.com/?i=14 would get the 14th line in my list and place it where desired.
How can I achieve this?
I'm not sure what you mean by "call a line of text," but maybe you could do this:
var url = window.location.href;
var queryPos = url.indexOf('i=');
var param = url.substr(queryPos + 'i='.length);
Now param will contain the value of the parameter and you could use it to fetch whatever.
But since you're trying to access a value from a URL with JavaScript, it might be better to make use of # as explained here: How do I get the value after hash (#) from a URL using jquery (there are non-jquery answers as well)
Hopefully this is what you need.
To place an array element where you need it on document load
<div id="placeHere"></div>
In JS
document.body.onload = function(){
document.getElementById('placeHere').innerHTML = array[14];
}
jQuery
$(document).ready(function(){
$('#placeHere').html(array[14]);
})

JS split OR Logical Operator

User inputs a web address that I want to get only the tail from, as I do know what site he inputs.
So first I want to remove the "main" URL and get what ever is at the end, so my action is:
Original link: http://example.com/something
var n=e.split("http://example.com/");e=n[1];
And I will get "something"
The problem is that site can also be secured, thus having https not http. Therefore the split wont work.
How do I define a split function, that would work like this:
split("http://example.com/ || https://example.com/")
I do not want to split by looking at "//" or anything of that sort, I want an exact address.
If you like it clear and want to avoid regular expressions, try this:
var n=e.split("http://example.com/",2).pop().split("https://example.com/",2).pop();
If you wish to know the host you can do so by using this code instead in JavaScript:
window.location.host
Source Get The Current Domain Name With Javascript (Not the path, etc.)
You can also use window.location.path to get the URL that was requested, combining those you get:
window.location.host + window.location.pathname
For me, this outputs stackoverflow.com/posts/25203020/edit while writing this reply.
var s = "http://example.com/something";
function split (url) {
var r = /([^:]+):\/\/([^\/]+)\/(.*)/gi;
var a = r.exec(url)
return [a[1], a[2], a[3]];
}

send text-field's value to google as search query

Is there a way I can get the value of an input-field and send that to Google as a search query ?
How I would go about doing this would be using jQuery's .attr() and storing that in a variable. Then replacing all the spaces in that string with pluses (+). Then attaching that to http://google.com/? and then navigating yo the newly constructed URL using window.location.
Is there a better way to do this?
Or, if you can not think of a better way, could you help me implement the above.
The parts I am having trouble with are using .attr() and giving window.location a variable as its argument / parametre.
Well, replacing spaces with + need not be done as it does not make your query any different.
For the rest, you can use:
window.location.href = 'http://google.com?q='+encodeUriComponent($('#textbox').val());

What's an easy way to get the url in the current window minus the domain name?

My Javascript ain't so hot, so before I get into some messy string operations, I thought I'd ask:
If the current url is: "http://stackoverflow.com/questions/ask"
What's a good way to to just get: "/questions/ask" ?
Basically I want a string that matches the Url without the domain or the "http://"
alert(window.location.pathname);
Here's some documentation for you for window.location.
ADDITIONAL ANSWER:
window.location.pathname itself is just not enough because it doesn't include the query part, and also URN if exists:
Sample URI = "http://some.domain/path-value?query=string#testURN"
window.location.pathname result = "/path-value"
window.location.search result = "?query=string"
pathname + search result = "/path-value?query=string"
If you want to get all the values just except the domain name, you can use the following code:
window.location.href.replace(window.location.origin, "")
Or as #Maickel suggested, with a simpler syntax:
window.location.href.substring(window.location.origin.length);
This gets the following URL parts correctly:
http://some.domain/path-value?query=string#testURN
alert(window.location.href.replace(window.location.origin, ""))--> "/path-value?query=string#testURN"
Use window.location.pathname.

Categories