drgm51600 2016-07-01 16:06
浏览 25
已采纳

我可以将javascript放在带有src的脚本标记的括号内吗? [重复]

This question already has an answer here:

I use these variables assigned by PHP in my linked javascript file below. Just wondering if its ok to do this:

<script src="js/settings.js">
var chores = <?php if ($chores == 0) echo "false"; else echo "true"; ?>;
var contacts = <?php if ($contacts == 0) echo "false"; else echo "true"; ?>;
var gyms = <?php if ($gyms == 0) echo "false"; else echo "true"; ?>;
var meetings = <?php if ($meetings == 0) echo "false"; else echo "true"; ?>;
var spirituals = <?php if ($spirituals == 0) echo "false"; else echo "true"; ?>;
</script>

Or should I do this:

<script>
    var chores = <?php if ($chores == 0) echo "false"; else echo "true"; ?>;
    var contacts = <?php if ($contacts == 0) echo "false"; else echo "true"; ?>;
    var gyms = <?php if ($gyms == 0) echo "false"; else echo "true"; ?>;
    var meetings = <?php if ($meetings == 0) echo "false"; else echo "true"; ?>;
    var spirituals = <?php if ($spirituals == 0) echo "false"; else echo "true"; ?>;
</script>
<script src="js/settings.js"></script>

Both seem to work fine. I don't know why I'm nervous about the top one.

</div>
  • 写回答

1条回答 默认 最新

  • dtgu21994537 2016-07-01 16:12
    关注

    If the js file (js/settings.js) uses the variables assigned in the document then you should include the javascript file after the variables have been assigned otherwise you're likely to get 'undefined' error.

    If js/settings.js use something like $(document).ready(... in which the variables are used after the document is loaded then either way would be fine.

    Ideally, in my opinion you should always include the script files after

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行