JavaScript code in source doesn't reflect code that is actually executed

眉间皱痕 提交于 2019-12-12 06:54:13

问题


My javascript code appears to work as it's supposed to. However, when I 'view source' in Chrome, it disagrees with the javascript that is actually executed.

Here is my code:

<?php
    $_SESSION['new'] = "blue";
    if (!isset($_SESSION['old'])) { $_SESSION['old'] = "blue"; }
        echo '<script type="text/javascript">
            $(document).ready(function() {
                changeCol("'.$_SESSION["old"].'","'.$_SESSION["new"].'");
            });
          </script>';
    $_SESSION['old'] = "blue";
?>    

$_SESSION['old']="green" from the previous page. The code is supposed to call changeCol("green","blue"), and then set $_SESSION['old']="blue".

In fact, both of these things happen, so my code works as it's designed, but if I view source, it says changeCol("blue","blue"). This is strange, because if in changeCol() I write the passed variables to console.log, I get green, blue.

So if it's calling changeCol(green,blue) why does it say changeCol(blue,blue) when I view source?


回答1:


When you view the source, you're probably making an additional request. Your session variable will be reset.

If you're using Chrome or Firefox — which you should be — you can open up either the Web Developer Tools or Firebug and examine the actual DOM tree. (This is also pretty useful in situations where a script has added content dynamically.)




回答2:


Did you include <?php session_start(); ?> on your second page?



来源:https://stackoverflow.com/questions/13756897/javascript-code-in-source-doesnt-reflect-code-that-is-actually-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!