I want to get the location in a URL (localhost: port/: location). For example, if the URL is localhost:8080/show/myplace/ I want to extract myplace.
How to do this in javascript?
Try below code
var location = window.location.pathname.split('/').slice(-1)[0];
This will return the part of URI you want.
I think that :
window.location.pathname
should work
This will give you an array of each pathname in the url:
window.location.pathname.split('/')
https://jsfiddle.net/mhd28cre/ (check your console log when running the example)
Related
this.$route.path gets the URL without the hashcode at the end. How do I either get the hash-part of the URL or get the full URL so I can figure out how to separate the hash part?
To be clear, for an URL like https://example.com#test, I'm looking the get the test part in a Nuxt page.
Couldn't seem to find documentation for all stuff in $route to see if this is something that is available.
You can achieve this with the following
<script>
export default {
mounted() {
console.log('mounted', this.$route.hash)
},
}
</script>
In case of the following URL: http://localhost:3000/welcome-home#hello, it will print #hello to the console.
If you don't want the hash symbol, you can also do the following to only have hello
this.$route.hash.slice(1)
Here is the $route object in that case
Here is the doc regarding the properties: https://v3.router.vuejs.org/api/#route-object-properties
We can use string match() here:
var url = "https://example.com#test";
var hashtag = url.match(/[^#]+$/)[0];
console.log(hashtag);
I have a long list of urls to be modified.
For example the url is:
https://somedomain.com/?pid=24787CL22311&source=linkkit&url=https%3A%2F%2Fwww.demo.com
So in the above url I want to add subid=1233 just before the &source=linkkit
like this:
https://some.com/?pid=24787CL22311&subid=1233&source=linkkit&url=https%3A%2F%2Fwww.demo.com
I tried with append, set and replace but did not gave the specific result
Any suggestions ?
You can do something like:
let url = 'https://some.com/?pid=24787CL22311&subid=1233&source=linkkit&url=https%3A%2F%2Fwww.demo.com';
url = url.replace('&source=', '&subid=1233&source=')
what i want to do is simple, let's say I have a URL like this https://www.example.com/pro?type=oil coming from the URL it could anything like /pro?type=beans or /pro?type=rice and what I want is to get the URL redirect it to something like this https://www.example.com/zpro?type=oil. PLEASE THE FIRST URL is /pro? the redirect location is /zpro? Thank you.
If I understand what you want correctly, the below should do it.
window.location.href = window.location.href.replace('/pro?', '/zpro?')
So you can use window.location.href to access the string of the current URL. By setting it to a new value, you can change the current URL! So for you, that means setting window.location.href = window.location.href.replace('/pro', '/zpro'). This will take the string /pro in your URL and redirect to /zpro while keeping your query parameters intact!
I am facing issue in angular js, right now we have two urls in our application,
http://localhost/xyz?page=documents&view=grid&sortorder=desc&sortby=updatedate&limit=35&offset=0
and then we another url
http://localhost/abc
When i move from the first url to the second url it carries the query params from the first url, this is how the second url looks like
http://localhost/abc?page=documents&view=grid&sortorder=desc&sortby=updatedate&limit=35&offset=35
We don't the fetch url carrying the query params from the first page. I am new to angular js, I have came across few options like
$location.search({});
$location.url($location.path())
But those didn't work at all.
I think i know what you mean, to remove parameters use
$location.url($location.path());
Hope it helps
Check this documentation for the location with angular
1.Save the query object $location.search()in some place (local storage or cookies), then in the target controller $.map(query,funcion(k,v){ $location.search(k,v});
2.Dynamically append to the end url2 + $location.path() in href attribute
$location.url changes path, search and hash.
So, $location.url('new_path') should work!
I have an url like this:
site.co/asd#antani?id=9
How do i get id value from this url dynamically and get the new value when it changes?
i tryed :
console.log($routeParams); and i got Object {} in console
thanks
Since I don't know your full Env. I would go to this dirtection:
var s = $location.search('site.co/asd#antani?id=9');
console.log(s.$$search);
Output:
Object {site.co/asd#antani?id: "9"}
Hope it will help,
OK i think i fixed it and it was to change URL
so to make $routeParams works i needed to call
site.com/asd?id=9#antani
instead of
site.co/asd#antani?id=9
hope it can help also if i don't like the url in that way :D
big thanks to all and if you have any better solution let me know please !
Got a gist to do that and other useful functions on urls with params ( URL params to object, Test if url Has Params, get Hash without params, and Change url params)
https://gist.github.com/dazzer13/10470514
You can do this:
var params = urlGetParams(url);
params.id // returns 9
var myURL = "site.co/asd#antani?id=9";
var myURL_id = myURL.split("#")[1];
Assuming this is the actual page you are on, you can use angular $routeParams. Something like this:
var id = $routeParams.id