我尝试做的是在不使用url中的hashtag元素的情况下获取单个字符串。我已经有一个正常运行的代码,但它需要改变。那么,如何在?
之后获取网址的任何部分。
说我有?fx=shipment+toys/fish-fix-fx/
作为我的网址字符串;我希望按钮显示shipment
或fish
或fx
是否是我选择的选项。
带有hastag的按钮:http://jsfiddle.net/66kCf/2/show/#iphone
原始JSFiddle(按钮未显示):http://jsfiddle.net/66kCf/2/
我希望iPhone按钮显示fix
是否为我的选择:http://jsfiddle.net/66kCf/2/show/?fx=shipment+toys/fish-fix-fx/
答案 0 :(得分:2)
尝试使用.split()
和.match()
这样做......
var keys = window.location.href.split('?');
if (keys[1].match(/(fix|fish|fx)/))
{
$("#linkdiv").append(nextLink);
$("#linkdiv1").append(nextLink);
$("#linkdiv2").append(nextLink);
}
演示按钮显示:http://jsfiddle.net/LbKmf/show/?fx=shipment+toys/fish-fix-fx/
演示按钮未显示:http://jsfiddle.net/LbKmf/show/?reigel
答案 1 :(得分:1)
这就是你要找的东西:
"?fx=shipment+toys/fish-fix-fx/".split(/[\?=+\/-]/g);
答案 2 :(得分:1)
通常,Javascript没有查询字符串参数的内置功能。您可以在window.location.search上使用字符串操作来从URL字符串中获取参数。请注意,location.search
也包含?
字符。
这样的事情应该做:
var queryString = function () {
// Anonymous function - executed immediately
// get rid of the '?' char
var str = "?fx=shipment+toys/fish-fix-fx/";
var query = str.substring(str.lastIndexOf('=')+1,str.indexOf('/'));
var vars = query.split("+");
for (var i=0;i<vars.length;i++){
console.log(vars[i]);
}
return vars;
} ();
答案 3 :(得分:0)