Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...