doumowu7371 2010-02-21 21:19
浏览 63
已采纳

在If语句中可以在Javascript中包含Php变量或URL吗?

I want to make some thing like this

my-php-file.php

  $lang = 'es';

my-js-file.js

if ($lang == es)
   {
   something-magical-happens;
   }

or like this:

if (URL == www.mydomain.com/index.php?lang=es)
   {
   something-magical-happens;
   }
  • 写回答

3条回答 默认 最新

  • dtnqbre7980007 2010-02-21 21:29
    关注

    you could generate js on-the-fly

    my.js.php:

    <?php echo "//Yes." ?>
    var i = "<?php echo $_GET['lang']; ?>";
    
    function doSomethingWithI(){
      alert(i);
    }
    

    Now, try to include

    <script type="text/javascript" src="my.js.php?lang=es"></script>
    

    in your html and you'll see :)

    Edit: Check it in action here: http://h.kissyour.net/so/phpjs/

    Edit: Edited example on my server to closer resemble what I wrote here.

    Edit: Oh yes. Don't forget to clean your code!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 CSS实现渐隐虚线框
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容