问题
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