2

我找到了这段代码,它很有效:

$.urlParam = function(name){
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) { return 0; }
    return results[1] || 0;}
}

因此,如果 url/查询字符串是 xyz.com/index.html?lang=de

只需调用 var langval = $.urlParam('lang'); 你明白了

--

我的信息来自单击图像,因此我创建了以下代码:

$('#admin-slideshow img').click(function() {
    alert($(this).attr('src'));
}

所以如果代码是:

<a href="#"><img src="image.php?url=image.jpg&tid=1&opn=1" /></a> 

它会提醒这一点(image.php?url=image.jpg&tid=1&opn=1)。

我的聪明想法是添加代码片段 $(this).attr('src'); 并将其替换为 window.location.href。它不起作用。有什么建议么?

$('#admin-slideshow img').click(function() {
    $.urlParam = function(name){
        var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec($(this).attr('src'));
        if (!results) { return 0; }
        return results[1] || 0;}
    }
}
4

2 回答 2

2

try this,

$.urlParam = function(name, src){
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec( src || window.location.href); // if src is undefined defualt is window.location.href
    if (!results) { return 0; }
    return results[1] || 0;
}

use it as

$.urlParam('lang',window.location.href); // for url or just $.urlParam('lang');
$.urlParam('lang',$('#admin-slideshow img').attr('src')); // for images

$('#admin-slideshow img').click(function() {
    alert($.urlParam('opn',this.src));
}
于 2010-08-26T08:16:59.230 回答
1
function urlParam( name ){
    var results = new RegExp( '[\\?&]' + name + '=([^&#]*)' ).exec( window.location.href );
    return ( results != undefined ) ? results[1] : false;

}

Removing js errors when url param not found.

于 2012-06-11T22:16:10.410 回答