0

I've a variable called url as follows :

url = "http://56.177.59.250/static/ajax.php?core[ajax]=true&core[call]=prj_name.contactform&width=400&core[security_token]=c7854c13380a26ff009a5cd9e6699840"

Now I want to use if condition only if core[call] is equal to the value it currently has i.e. prj_name.contactform otherwise not.

How should I do this since the parameter from query-string is in array format?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
PHPLover
  • 1
  • 51
  • 158
  • 311
  • 3
    parse query string, look for value. I dont quite see the problem here. – Fallenreaper Dec 31 '14 at 14:37
  • possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Hacketo Dec 31 '14 at 14:38

2 Answers2

3

Just use String.indexOf and check if it is present (that is not -1, which means it doesn't exist)

if(url.indexOf("core[call]=prj_name.contactform") > -1){
   // valid. Brew some code here
}
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • i like this. It is simple. No need to convert and create a plethora of complicated objects, therefore it doesn't take up extra space, and only scans once, unlike parsers which will need to scan repetitively for keywords like '?' or '&'. This i feel is the best, and straightforward approach. – Fallenreaper Dec 31 '14 at 14:43
0

You can use location.search :

 <script>
function get(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
if(get("core[call]") == "prj_name.contactform"){
    alert('ok');
}else{
    alert('no');
}
</script>
Invicnaper
  • 21
  • 4