[英]JavaScript code in source doesn't reflect code that is actually executed
My javascript code appears to work as it's supposed to. 我的JavaScript代码似乎可以正常运行。 However, when I 'view source' in Chrome, it disagrees with the javascript that is actually executed.
但是,当我在Chrome中“查看源代码”时,它与实际执行的javascript不同。
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. $_SESSION['old']="green"
来自上一页。 The code is supposed to call changeCol("green","blue")
, and then set $_SESSION['old']="blue"
. 该代码应该调用
changeCol("green","blue")
,然后设置$_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")
. 实际上,这两种情况都会发生,因此我的代码可以按照设计的方式工作, 但是如果查看源代码,它将显示为
changeCol("blue","blue")
。 This is strange, because if in changeCol() I write the passed variables to console.log
, I get green, blue
. 这很奇怪,因为如果在changeCol()中,我将传递的变量写入
console.log
,则会得到green, blue
。
So if it's calling changeCol(green,blue)
why does it say changeCol(blue,blue)
when I view source? 因此,如果它调用
changeCol(green,blue)
为什么在查看源代码时会说changeCol(blue,blue)
?
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. 如果您使用的是Chrome或Firefox,则可以打开Web开发工具或Firebug并检查实际的DOM树。 (This is also pretty useful in situations where a script has added content dynamically.)
(这在脚本动态添加内容的情况下也很有用。)
Did you include <?php session_start(); ?>
您是否包含
<?php session_start(); ?>
<?php session_start(); ?>
on your second page? <?php session_start(); ?>
在第二页上?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.