获取javascript中url设置的php变量的值

获取javascript中url设置的php变量的值,javascript,php,jquery,url,Javascript,Php,Jquery,Url,嘿,我在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

嘿,我在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    
    }
如果(“;”==null){
//因为变量是通过url设置的,所以请执行某些操作
}
否则{
//执行其他操作,因为变量不是通过url设置的
}
我使用的url如下:www.example.com/index.php?v=12345

当我用这个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    
}

试试这个

您可以使用javascript来获取查询字符串参数,而不是php。这附近有很多教程

参考:


可能的解决方案:

将其分配给任何javascript变量。var v=;我需要为此修改.htaccess吗?不需要更改htaccess我已经尝试过很多次了。。如果url中没有设置任何内容,则不会更改某些内容,但如果url类似于www.example.com/index.php?v=1234,则返回?v,1234。。。我只想退回1234。。我该怎么做呢?哦,好的,我知道了。看吧,小提琴帮了忙,我把它和“document.URL”混合在一起,得到了URL。。非常感谢。