简体   繁体   English

使用Javascript / jQuery在基于段的URL中获取查询字符串

[英]Using Javascript/jQuery to get Query String in Segment-based URL

I'm using a PHP framework Codeigniter that uses segment based urls like http://www.mydomain.com/age/11/name/john/color/red instead of the usual querystrings lijke http://www.mydomain.com/index.php?age=11&name=john&color=red . 我正在使用PHP框架Codeigniter,它使用基于段的URL,如http://www.mydomain.com/age/11/name/john/color/red而不是通常的查询字符串http://www.mydomain.com/index.php?age=11&name=john&color=red

How do I grab the value of the age key from the url using Javascript/jQuery? 如何使用Javascript / jQuery从URL获取age键的值?

After grabbing the value 11 I will pass it into a jQuery object when an event is fired. 在获取值11我将在触发事件时将其传递给jQuery对象。

$( "#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.com/" + age); 
            /* passing the value of age into URL to redirect to */
        }
    });

UPDATE UPDATE

  • The key/value pairs can change position , and the number of key/value pairs in the URL are not fixed 键/值对可以改变位置 ,并且URL中的键/值对的数量不固定
  • 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.com/age/20/name/john/color/red 我需要在Javascript中获取值因为用户使用滑块来更改特定值,例如age ,并且滑块中的新值20将用于将用户重定向到http://www.mydomain.com/age/20/name/john/color/red

You could try using the jQuery URL parser plugin. 您可以尝试使用jQuery URL解析器插件。

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. 使用.segment(n)调用来搜索"age"的URL路径,并获取下一个应包含您要查找的年龄值的路径。

Or try the .fsegment(n) method if age is always in the same position. 或者,如果age总是在同一位置,请尝试使用.fsegment(n)方法。

Or use .segment() with something like: 或者使用.segment()

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. 如果您只想要age部分,则不必将所有内容填入字典中。 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 . 然后,您可以使用parts.age访问age值。

before your code is run, add the following to your php script: 在运行代码之前,将以下内容添加到php脚本中:

<?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 编辑:请确保您有一些安全的显示$ _GET [“年龄”],你的JS内容,当你在.slider()事件检查GET变量是否存在会更好之前检查

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. 您可以使用window.location.href变量,该变量将为您提供整个当前URL,然后稍微使用该字符串来获取所需信息。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM