Angular js - get url query params when url has # hash - javascript

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

Related

Modify URL and add parameter in between the URL using javascript

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=')

How to get parameter from url in js?

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)

Redirect to a specific URL with Javascript

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!

Angular js Remove query parameters when changing the url

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!

How to get only the Controller name and action name from URL with jQuery

I am using .net MVC in my website and I need to get the URL in javascript, but only the controller and action name, for instance if i have:
http://stackoverflow.com/questions/9513736/current-url-without-parameters-hash-https
I need:
http://stackoverflow.com/questions/9513736/
I have found some solutions such as:
urlBase = location.href.substring(0, location.href.lastIndexOf("/")+1)
->http://stackoverflow.com/questions/9513736/
However if my URL does not have any parameters I the action name is cut off like so:
http://stackoverflow.com/questions/
Does anyone have a solution for this?
You can use
window.location.pathname
for this.
For the url in this question it returns
"/questions/28946447/how-to-get-only-the-controller-name-and-action-name-from-url-with-jquery"
To read it properly you can use: window.location.pathname.split("/")
Read the value with:
var url = window.location.pathname.split("/");
var questions = url[1];
window.location.pathname;
this will return path from the url . then use split to get controller name and action name
var path=window.location.pathname;
var abc=path.split("/");
var controller=abc[0];
var action=abc[1] || "index";

Categories