Javascript document.write overwriting a php page - Stack Overflow

I have this Javascript function:function capitalizeFL(string) { takes a string, returns it with first

I have this Javascript function:

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:

<?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:

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.

I'm assuming the problem is due to document.write overwriting the whole page. The question is, how can I do the following, without using document.write?

I have this Javascript function:

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:

<?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:

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.

I'm assuming the problem is due to document.write overwriting the whole page. The question is, how can I do the following, without using document.write?

Share Improve this question asked May 16, 2013 at 16:12 frrlodfrrlod 6,7158 gold badges33 silver badges38 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 7

You can not use document.write after page load. 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. Just use the script tags. jQuery will handle the script tags for you.

You really should just skip using load and use $.get or $.getJSON and handle the response yourself.

Have the server return a JSON object.

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

and the JavaScript would be

$.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. There is no reason for JavaScript to do it.

No JavaScript needed!

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

and the jQuery load would be the same

 $("#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

I'm assuming the problem is due to document.write overwriting the whole page. The question is, how can I do the following, without using document.write?

Because is exactly what document.write does.

Try with innerHTML:

For example, if you want to add content to #topMenu, just do:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745348489a4623680.html

相关推荐

  • Javascript document.write overwriting a php page - Stack Overflow

    I have this Javascript function:function capitalizeFL(string) { takes a string, returns it with first

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信