Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
29
rated 0 times [  35] [ 6]  / answers: 1 / hits: 35648  / 9 Years ago, thu, april 9, 2015, 12:00:00

I am trying to get query string values using angularjs.



my Url: http://localhost/example.php?sportsId=3



when I applied var goto = $location.search()['sportsId'];
it returns me undefined.



However, if I add hash in url like Url: http://localhost/example.php#?sportsId=3



then it returns me correct value 3.



But in this case, it also gives me Error: [$parse:syntax] http://errors.angularjs.org/1.3.8/$parse/syntax?p0=undefined&p1=not%20a%20primary%20expression&p2=null&p3=sportsID%3D&p4=sportsID%3D



Also, my default $_REQUEST['sportsId'] is not working with hash format.



How can I correctly get values from query string by using angularjs?


More From » php

 Answers
89

I know this is not Angular but its pure JS and works like a charm (just don't add dummyPath and it will take the URL).



function getUrlParameter(param, dummyPath) {
var sPageURL = dummyPath || window.location.search.substring(1),
sURLVariables = sPageURL.split(/[&||?]/),
res;

for (var i = 0; i < sURLVariables.length; i += 1) {
var paramName = sURLVariables[i],
sParameterName = (paramName || '').split('=');

if (sParameterName[0] === param) {
res = sParameterName[1];
}
}

return res;
}


Usage:



var sportdsId = getUrlParameter('sportsId');

[#67136] Tuesday, April 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
patriciakiarrac

Total Points: 532
Total Questions: 100
Total Answers: 89

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
;