为什么javascript无法使用我在php文件中声明的javascript变量?

嘿大家,这个问题让我在上周左右难过了,情况如下:</ p>

我有一个使用GoDaddy托管托管的网站。 本期中使用的三个文件是index.html,milktruck.js和xml_http_request.php,它们都位于同一目录中。</ p>

index.html文件引用了milktruck.js 文件包含以下代码:</ p>

 &lt; script type =“text / javascript”src =“milktruck.js”&gt;&lt; / script&gt;

</ code> </ pre>

打开网站时会自动触发milktruck.js文件。 此时xml_http_request.php尚未触发。</ p>

在2000行的第79行中,我将变量“simple”传递给milktruck.js文件中的函数:</ p>

  placem('p2','pp2',simple,window ['lla0_2'],window ['lla1_2'],window ['lla2_2']); 
</ 代码> </ pre>

“simple”从未在milktruck.js文件中初始化。 相反,我在xml_http_request.php文件中包含以下代码行:</ p>

  echo“&lt; script&gt; var simple ='string o text';&lt; / script&gt  ;“; 
</ code> </ pre>

此时我还没有对milktruck.js文件中的xml_http_request.php文件做任何引用。 我没有引用该文件直到milktruck.js文件的第661行,其中包含以下代码行:</ p>

  xmlhttp.open('GET',“xml_http_request.php?  pid =“+ pid +”&amp; unLoader = true“,false); 
</ code> </ pre>

所有内容都编译(我假设因为我的游戏运行),但是placem函数 由于字符串'string o text'从不显示,因此无法正常运行。 </ p>

如果我要在php文件中注释出初始化“simple”的代码行,并在我调用函数placem之前包含以下代码行,那么一切正常并且文本正常 显示:</ p>

  var simple ='string o text'; 
</ code> </ pre>

您认为问题出在哪里? 这里? 在尝试使用javascript文件中的“简单”变量之前,是否需要调用php文件? 我该怎么办? 或者我的代码有问题吗?</ p>
</ div>

展开原文

原文

Hey everybody, this issue has had me stumped for the last week or so, here's the situation:

I've got a site hosted using GoDaddy hosting. The three files used in this issue are index.html , milktruck.js , and xml_http_request.php all hosted in the same directory.

The index.html file makes reference to the milktruck.js file with the following code:

 <script type="text/javascript" src="milktruck.js"></script> 

The milktruck.js file automatically fires when the site is opened. The xml_http_request.php has not fired at this point.

On line 79 out of 2000 I'm passing the variable "simple" to a function within the milktruck.js file with:

 placem('p2','pp2', simple, window['lla0_2'],window['lla1_2'],window['lla2_2']);

"simple" was never initialized within the milktruck.js file. Instead I've included the following line of code in the xml_http_request.php file:

 echo "<script> var simple = 'string o text'; </script>";

At this point I have not made any reference whatsoever to the xml_http_request.php file within the milktruck.js file. I don't reference that file until line 661 of the milktruck.js file with the following line of code:

 xmlhttp.open('GET',"xml_http_request.php?pid="+pid+"&unLoader=true", false);

Everything compiles (I'm assuming because my game runs) , however the placem function doesn't run properly because the string 'string o text' never shows up.

If I was to comment out the line of code within the php file initializing "simple" and include the following line of code just before I call the function placem, everything works fine and the text shows up:

 var simple = 'string o text';

Where do you think the problem is here? Do I need to call the php file before I try using the "simple" variable in the javascript file? How would I do that? Or is there something wrong with my code?

downloadbooks_2014
downloadbooks_2014 我不是说你提出问题的愿望,而是你提出问题的方式。你错过了对一些基本概念的理解,但似乎你不愿意学习它们。相反,您只想找到解决您当前问题的方法。
9 年多之前 回复
douwei2713
douwei2713 我确实想要理解,但是我不会理解我的头撞墙。如果我被困住了,我需要帮助,一旦有人帮助了它,就会增加我的理解力。就像我现在理解的那样,我的index.html文件中的<!-#includeFILE=“xml_http_request.php”->这一行在全世界都有所不同。
9 年多之前 回复
dousuohe5882
dousuohe5882 你想了解还是只想让别人为你解决?
9 年多之前 回复
duanjurong1347
duanjurong1347 这是我的索引和js文件的链接。我不知道如何显示xml_http_request的代码:alterearth.net,alterearth.net/milktruck.js
9 年多之前 回复
doujia3441
doujia3441 编辑你的原始问题,不要只是每次你顿悟时产生一个新的问题(以前的类似问题没有得到解答)。
9 年多之前 回复
dongxiaoyan4388
dongxiaoyan4388 如果您对架构有基本的了解,那将会有所帮助。(我不是想在这里粗鲁,但你不了解HTTP请求的基本流程。)你是否在其他一个问题中阅读了我的答案?我试着解释一下。
9 年多之前 回复
dougou6727
dougou6727 是的,包括一些代码,我会很乐意帮助...上帝保佑不要提出新的问题......
9 年多之前 回复
doushan6161
doushan6161 我如何调用php文件?
9 年多之前 回复
dsyk33753
dsyk33753 是的,但我每次都试图让它更具体。如果我包含所有文件供人们查看,会有帮助吗?
9 年多之前 回复
doupang9080
doupang9080 上次我检查变量意味着在使用之前初始化:/,所以如果你需要在第2行的js文件中放置一个变量,那么你将在第1行调用php文件...
9 年多之前 回复
duanjian4698
duanjian4698 你是否意识到你基本上问过同样的问题3次?
9 年多之前 回复
dsy19811981
dsy19811981 echo“<script>varsimple='stringotext';</script>”;-你在哪里回应这个?也许你已经在SCRIPT区块内了。在这种情况下,你必须这样回声:echo“varsimple='stringotext';”;
9 年多之前 回复
duandou8120
duandou8120 Javascript无法编译。你只知道它没有正确运行,好吧,它没有像你期望的那样运行,或者,你处理错误。
9 年多之前 回复
drwjv28028
drwjv28028 使用Firebug或标准的Firefox错误控制台。他们会告诉您脚本是否尝试访问未声明的变量。
9 年多之前 回复

2个回答

So, we meet again!

Buried in the question comments is the link to the actual Javascript file. It's 2,200 lines, 73kb, and poorly formatted. It's also derived from a demo for the Google Earth API.

As noted in both the comments here and in previous questions, you may be suffering from a fundamental misunderstanding about how PHP works, and how PHP interacts with Javascript.

Let's take a look at lines 62-67 of milktruck.js:

//experiment with php and javascript interaction

//'<?php $simpleString = "i hope this works"; ?>'


//var simple = "<?php echo $simpleString; ?>";

The reason this never worked is because files with the .js extension are not processed by PHP without doing some bizarre configuration changes on your server. Being on shared hosting, you won't be able to do that. Instead, you can rename the file with the .php extension. This will allow PHP to process the file, and allow the commands you entered to actually work.

You will need to make one more change to the file. At the very top, the very very top, before anything else, you will need the following line:

<?php header('Content-Type: text/javascript'); ?>

This command will tell the browser that the file being returned is Javascript. This is needed because PHP normally outputs HTML, not Javascript. Some browsers will not recognize the script if it isn't identified as Javascript.

Now that we've got that out of the way...

Instead I've included the following line of code in the xml_http_request.php file: <a script tag>

This is very unlikely to work. If it does work, it's probably by accident. We're not dealing with a normal ajax library here. We're dealing with some wacky thing created by the Google Earth folks a very, very long time ago.

Except for one or two in that entire monolithic chunk of code, there are no ajax requests that actually process the result. This means that it's unlikely that the script tag could be processed. Further, the one or two that do process the result actually treat it as XML and return a document. It's very unlikely that the script tag is processed there either.

This is going to explain why the variable never shows up reliably in Javascript.

If you need to return executable code from your ajax calls, and do so reliably, you'll want to adopt a mature, well-tested Javascript library like jQuery. Don't worry, you can mix and match the existing code and jQuery if you really wanted to. There's an API call just to load additional scripts. If you just wanted to return data, that's what JSON is for. You can have PHP code emit JSON and have jQuery fetch it. That's a heck of a lot faster, easier, and more convenient than your current unfortunate mess.

Oh, and get Firebug or use Chrome / Safari's dev tools, they will save you a great deal of Javascript pain.

However...

I'm going to be very frank here. This is bad code. This is horrible code. It's poorly formatted, the commenting is a joke, and there are roughly one point seven billion global variables. The code scares me. It scares me deeply. I would be hesitant to touch it with a ten foot pole.

I would not wish maintenance of this code on my worst enemy, and here you are, trying to do something odd with it.

I heartily encourage you to hone your skills on a codebase that is less archaic and obtuse than this one before returning to this project. Save your sanity, get out while you still can!



或许初始化你的值如下:
window.simple ='blah blah blah'</ p>

然后传递window.simple </ p>

您可以尝试调试器来查看发生了什么,例如。 FireBug </ p>
</ div>

展开原文

原文

perhaps init your values like this: window.simple = 'blah blah blah'

then pass window.simple

You could try the debugger to see what is going on, eg. FireBug

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐