php - Using JavascriptjQuery to get Query String in Segment-based URL - Stack Overflow

I'm using a PHP framework Codeigniter that uses segment based urls likeinstead of the usual quer

I'm using a PHP framework Codeigniter that uses segment based urls like instead of the usual querystrings lijke .php?age=11&name=john&color=red.

How do I grab the value of the age key from the url using Javascript/jQuery?

After grabbing the value 11 I will pass it into a jQuery object when an event is fired.

$( "#searchdistance_slider" ).slider({
        range: "min",
        value: 5,
        min: 0.5,
        max: 10,
        slide: function( event, ui ) {
            $( "#search_distance" ).val(ui.value + " miles");
            window.location.replace("/" + age); 
            /* passing the value of age into URL to redirect to */
        }
    });

UPDATE

  • The key/value pairs can change position, and the number of key/value pairs in the URL are not fixed
  • I need to grab the value within Javascript because the user uses a slider to change a particular value, say the age, and the new value 20 from the slider will be use to redirect the user to

I'm using a PHP framework Codeigniter that uses segment based urls like http://www.mydomain./age/11/name/john/color/red instead of the usual querystrings lijke http://www.mydomain./index.php?age=11&name=john&color=red.

How do I grab the value of the age key from the url using Javascript/jQuery?

After grabbing the value 11 I will pass it into a jQuery object when an event is fired.

$( "#searchdistance_slider" ).slider({
        range: "min",
        value: 5,
        min: 0.5,
        max: 10,
        slide: function( event, ui ) {
            $( "#search_distance" ).val(ui.value + " miles");
            window.location.replace("http://www.domain./" + age); 
            /* passing the value of age into URL to redirect to */
        }
    });

UPDATE

  • The key/value pairs can change position, and the number of key/value pairs in the URL are not fixed
  • I need to grab the value within Javascript because the user uses a slider to change a particular value, say the age, and the new value 20 from the slider will be use to redirect the user to http://www.mydomain./age/20/name/john/color/red
Share Improve this question edited Jun 26, 2011 at 17:05 Nyxynyx asked Jun 26, 2011 at 16:52 NyxynyxNyxynyx 63.9k163 gold badges507 silver badges856 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

You could try using the jQuery URL parser plugin.

Use the .segment(n) call to search the URL path for "age", and grab the next one which should contain the age value you're looking for.

Or try the .fsegment(n) method if age is always in the same position.

Or use .segment() with something like:

var parts = $.url(your_url).segment();
var params = [];
for (var i=0; i<parts.length/2; i++) {
   params[parts[2*i]] = parts[2*i+1];
}
var age = parseInt(params['age']);

(I'm sure there's a prettier way to do that though.)

If you only want the age part, you don't have to do all that stuffing into a dictionary. Just exit the loop early when you've found the key you want.

If you want to reconstruct the path with a changed age, try something like:

var parts = $.url(your_url).segment();
var newpath = '';
for (var i=0; i<parts.length/2; i++) {
  if (parts[2*i] == 'age') {
    newpath += '/age/' + (parseInt(parts[2*i+1]) + 42); // put your age modifiy
                                                        // code here
  } else {
    newpath += '/' + parts[2*i] + '/' + parts[2*i+1];
  }
}
alert(newpath);

You could split it with something like this:

var parts = {},
    bits = location.pathname.substr(1).split('/'); // get rid of first / and split on slashes

for (var i = 0; i<bits.length; i += 2) {
    parts[bits[i]] = bits[i+1];
}

You could then access the age value with parts.age.

before your code is run, add the following to your php script:

<?php
$getAgre = ( isset($_GET['age']) && strlen($_GET['age'])>0 )? $_GET['age'] : 'null';
echo '<script>var age = '. $getAge .';</script>';
?>

edit: make sure you have some security checking before displaying the content of $_GET['age'] and your JS would be better when your event in .slider() checked whether variable get exists

You could use window.location.href variable which would give you the whole current url, and then play with the string a bit to get the desired information. Substring, split, even regex.

This would work:

var ageMatch= window.location.href.match(/age\/([\d]*)/);
var age= ageMatch[1];

Just for the record, you can also update the url just as easily:

var newUrl= window.location.href.replace(/age\/([\d]*)/, age);
window.location.href= newUrl;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745242987a4618281.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信