Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript从链接获取参数_Javascript_Jquery_Html - Fatal编程技术网

Javascript从链接获取参数

Javascript从链接获取参数,javascript,jquery,html,Javascript,Jquery,Html,html代码 函数getQuerystring(){ var getUrlParameter=函数getUrlParameter(sParam){ var sPageURL=window.location.search.substring(1), sURLVariables=sPageURL.split(“&”), sParameterName, 我 对于(i=0;i

html代码

函数getQuerystring(){ var getUrlParameter=函数getUrlParameter(sParam){ var sPageURL=window.location.search.substring(1), sURLVariables=sPageURL.split(“&”), sParameterName, 我 对于(i=0;i
窗口。位置。搜索将:

  • 从当前页面的URL而不是链接到的页面提供数据
  • 从查询字符串(以
    开头,一直持续到片段的开头)而不是片段(以
    #
    开头,一直持续到结尾…即使片段包含
您需要使用
refToTheAnchor.search
并在属性中确定查询和片段的顺序

从链接获取参数

您的代码使用

var sPageURL = window.location.search.substring(1)
这不是“来自链接”,因此需要更改。正如您标记了jquery(尽管没有任何jquery),这里有一个可能的解决方案:

$(“#测试”)。单击(函数(){
getQuerystring(这个);
返回false;
});
函数getQuerystring(el){
console.log(el.href);
var getUrlParameter=函数(sParam){
var sPageURL=el.href.split(“?”)[1],
sURLVariables=sPageURL.split(“&”),
sParameterName;
对于(变量i=0;i

您可以尝试使用此函数,该函数使用正则表达式查找查询参数。将参数名传递给它以返回值

//to get query parameters.
$.urlParam = function (name) {
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec($(location).attr('href'));
    return (results) ? results[1] : "";
}
然后可以在代码中调用此方法,如下所示

var paramValue = $.urlParam('paramName');

如果不想使用location.href值,可以修改该方法以传递任何url。

您的代码工作正常。不在代码段中,因为它使用
location.search
,其中不包含
c=
,但如果您设置
var sPageURL=“c=active”然后它工作正常-因此假设您的window.location不包含
c=
。您的链接确实包含
c=
,并且您的注释是“应该在链接中显示c的值”-但是您的代码不会试图从链接中获取任何内容。请通过代码向我建议可能会对您有所帮助。我从这段代码中获得帮助。正如您所看到的,这是相同的代码。请指导我如何在同一页面上将值从javascript传递到php请指导我如何在同一页面上将值从javascript传递到php。我正在尝试使用cookiei-e“document.cookie=”blog=“+blog;”,我的php代码是$chk=$\u cookie['blog'];。但是它不起作用。如果你有新问题,请问一个新问题。