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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改