我今天遇到了以下网址:
http://www.sfgate.com/cgi-bin/blogs/inmarin/detail??blogid=122&entry_id=64497
Run Code Online (Sandbox Code Playgroud)
注意查询字符串开头的加倍问号:
??blogid=122&entry_id=64497
Run Code Online (Sandbox Code Playgroud)
我的浏览器似乎没有任何问题,并运行快速书签:
javascript:alert(document.location.search);
Run Code Online (Sandbox Code Playgroud)
刚刚给了我上面显示的查询字符串.
这是一个有效的URL吗?我之所以如此迂腐(假设我是)是因为我需要为查询参数解析这样的URL,并且支持加倍的问号需要对我的代码进行一些更改.显然,如果他们在野外,我需要支持他们; 我很好奇,如果我没有完全遵守URL标准,或者它实际上是非标准URL.
我一直在寻找一种有效的方法来做到这一点,但一直无法找到它,基本上我需要的是给定这个url例如:
http://localhost/mysite/includes/phpThumb.php?src=http://media2.jupix.co.uk/v3/clients/4/properties/795/IMG_795_1_large.jpg&w=592&aoe=1&q=100
Run Code Online (Sandbox Code Playgroud)
我希望能够src使用javascript或jquery使用其他值更改参数中的URL ,这可能吗?
提前致谢.
原始网址:
http://yourewebsite.php?id=10&color_id=1
Run Code Online (Sandbox Code Playgroud)
结果URL:
http://yourewebsite.php?id=10
Run Code Online (Sandbox Code Playgroud)
我得到了添加Param的功能
function insertParam(key, value){
key = escape(key); value = escape(value);
var kvp = document.location.search.substr(1).split('&');
var i=kvp.length; var x; while(i--)
{
x = kvp[i].split('=');
if (x[0]==key)
{
x[1] = value;
kvp[i] = x.join('=');
break;
}
}
if(i<0) {kvp[kvp.length] = [key,value].join('=');}
//this will reload the page, it's likely better to store this until finished
document.location.search = kvp.join('&');
}
Run Code Online (Sandbox Code Playgroud)
但我需要运行以删除Param
我有一个var存储字符串,代表一个充满参数的URL.我正在使用AngularJS,我不确定是否有任何有用的模块(或者可能使用普通的JavaScript)来删除不需要的URL参数而不必使用正则表达式?
例如,我需要删除&month=05以及&year=2017:
var url = "at merge ?derivate=21&gear_type__in=13&engine=73&month=05&year=2017"
我正在尝试创建一个函数来从URL中删除特定的查询字符串及其值.
例如:如果我有一个网址
var url = www.foo.com/test?name=kevin&gender=Male&id=1234
如果我传递名称 - >它应该删除名称的键和值.网址应该成为
www.foo.com/test?gender=Male&id=1234
我有一个功能 ReturnRefinedURL(key,url)
我在功能中这样做
function ReturnRefinedURL(key,url)
{
var Value = getParameterByName(key); // This returns kevin
var stringToBeRemoved = 'key +'='+ Value+'&'; // string becomes 'name=kevin&'
return url.replace(stringToBeRemoved, '');
}
Run Code Online (Sandbox Code Playgroud)
//在谷歌发现:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Run Code Online (Sandbox Code Playgroud)
所以当我打电话给方法时 ReturnRefinedURL('name',window.location.href);
这个工作!!! 但寻找更优雅和万无一失的方法.
*如果name参数是查询字符串中的第二个参数,则此方法不起作用.('&'仍将保留)