嘿,我在body标记中定义了一个php变量,如下所示:
<?php $v=null;?>
现在我用这个代码来检查变量是否是通过url 设置的
if ("<?php echo $_GET["v"];?>;" == null) {
// Do something because the variable is set through the url
}
else {
// Do something else because the variable is not set through the url
}
我使用的网址如下:www.example.com/index.php?v=1234
当我用这个url打开索引来运行这个代码时,它说v是未定义的。。当我用"www.example.com/index.php"打开索引来运行这段代码时,它也是未定义的。。但不是"空"
这里有什么问题?感谢
像一样尝试
var my_var_arr = location.search.replace('?', '').split('=');
if (my_var_arr[1] == null) {
// Do something because the variable is set through the url
}
else {
// Do something else because the variable is not set through the url
}
试试这个FIDDLE
您可以使用javascript来获取查询字符串参数,而不是php。这里有很多教程。
参考编号:http://javascriptproductivity.blogspot.in/2013/02/get-url-variables-with-javascript.html
可能的解决方案:jquery从URL 获取querystring