简体   繁体   English

Javascript document.write覆盖php页面

[英]Javascript document.write overwriting a php page

I have this Javascript function: 我有这个Javascript函数:

function capitalizeFL(string) { //takes a string, returns it with first letter capitalized
    return string.charAt(0).toUpperCase() + string.slice(1);
}

A file called statuswindow.php , which includes the following: 名为statuswindow.php的文件,其中包括以下内容:

<?php
    $raceV = "<script>document.write(capitalizeFL(\"".$player->race."\"));</script>";
    $clasV = "<script>document.write(capitalizeFL(\"".$player->clas."\"));</script>";
    echo "You have chosen a " . $raceV. " " .$clasV ."!";
?>

Now the main file, which uses ajax to update and show the player's class and race ( $clas , $race ), after capitalizing their first letters using capitalizeFL : 现在是主文件,在使用capitalizeFL将首字母capitalizeFL后,它使用ajax更新并显示玩家的职业和种族( $clas$race ):

Main file includes the following: 主文件包括以下内容:

$("button").click(function() {
  $("#topMenu").load("statuswindow.php");
});

What I would LIKE to happen, is that the html from statuswindow.php will be properly displayed in the main window's #topMenu div. 我想发生的是, statuswindow.php中的html将正确显示在主窗口的#topMenu div中。

I'm assuming the problem is due to document.write overwriting the whole page. 我假设问题是由于document.write覆盖了整个页面。 The question is, how can I do the following, without using document.write ? 问题是,如何在不使用document.write情况下执行以下操作?

You can not use document.write after page load. 页面加载后不能使用document.write。 what it does is opens up a new document and replaces what ever you have there with new content. 它的作用是打开一个新文档,并用新内容替换您现有的内容。

In this example there is no need to even use document.write. 在此示例中,甚至不需要使用document.write。 Just use the script tags. 只需使用脚本标签即可。 jQuery will handle the script tags for you . jQuery将为您处理脚本标签

You really should just skip using load and use $.get or $.getJSON and handle the response yourself. 您真的应该只跳过使用load并使用$ .get或$ .getJSON自己处理响应。

Have the server return a JSON object. 让服务器返回JSON对象。

{
  raceV : "foo",
  clasV : "bar",
  outStr : "You have chosen a {1} {2}!"
}

and the JavaScript would be 和JavaScript将是

$.getJSON("statuswindow.php", function(data) {
    var outString = data.outStr;
    outString = outString.replace("{1}",capitalizeFL(raceV));
    outString = outString.replace("{2}",capitalizeFL(clasV));
    $("#topMenu").html(outString );
})

BUT the real issue is: 但是真正的问题是:

Why are you not doing all of this in PHP. 为什么不使用PHP来完成所有这些工作。 There is no reason for JavaScript to do it. JavaScript没有理由这样做。

No JavaScript needed! 无需JavaScript!

<?php
    $raceV = ucfirst($player->race);
    $clasV = ucfirst($player->clas);
    echo "You have chosen a " . $raceV. " " .$clasV ."!";
?>

and the jQuery load would be the same 和jQuery的负载将是相同的

 $("#topMenu").load("statuswindow.php");
echo "You have chosen a ".ucFirst($player->race)...

Would make more sense 会更有意义

When you use $.load you do not really want any scripts in the page you load and in this case there is absolutely zero reason to have javascript uppercase the first letter when php has a built-in function to do it 当您使用$ .load时,您实际上并不需要页面中的任何脚本,在这种情况下,当php具有内置函数时,将javascript大写的首字母绝对是零的原因

I'm assuming the problem is due to document.write overwriting the whole page. 我假设问题是由于document.write覆盖了整个页面。 The question is, how can I do the following, without using document.write? 问题是,如何在不使用document.write的情况下执行以下操作?

Because is exactly what document.write does. 因为正是document.write所做的。

Try with innerHTML : 尝试使用innerHTML

For example, if you want to add content to #topMenu , just do: 例如,如果要向#topMenu添加内容,只需执行以下操作:

document.getElementById('topMenu').innerHTML += capitalizeFL(".$player->race.");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM