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

标签 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 the variable is not set through the url    
    }

我使用的网址如下:www.example.com/index.php?v=12345

当我通过使用此网址打开索引来运行此代码时,它说 v 未定义.. 当我通过使用“www.example.com/index.php”打开索引来运行此代码时,它也是未定义的..但不是“null”

这里有什么问题吗?谢谢

最佳答案

尝试一下

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 - 获取 javascript 中 url 设置的 php 变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20437980/

相关文章:

php - 如何将 PHP 库添加到我在 Eclipse 中的基本远程项目?

javascript - jQuery - 如何在事件触发后暂时禁用 onclick 事件监听器?

javascript - AJAX 成功不显示我的 PHP 错误或成功消息

jquery - Monkey Patch 选择了 jQuery 库 - 选择总是未定义

javascript - Chart.js 响应选项 - 图表未填充容器

javascript - 向上滚动文本并在使用 html5 音频读取文本时将其隐藏

javascript - ember.js 中自定义输入字段的最佳实践

javascript - 下载大文件

php - 警告: PDO::__construct(): [2002] 中没有这样的文件或目录(尝试通过 unix:///tmp/mysql.sock 连接)

带有发起者 ip 的 php curl post