0

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?

0

3 Answers 3

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");
Sign up to request clarification or add additional context in comments.

3 Comments

@mplungjan Imitation is the sincerest form of flattery
I did not even look at other posts, I went and get a sandwich and added the code. I had the text there to do it in PHP, I just did not have an example.
The real problem is the poster not understanding PHP and JS 101 stuff.
3
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

5 Comments

The only (good) reason to have scripts in $.load would be to dynamically hookup events to the HTML you're adding
Even then I would frown, and have event handlers in the success instead or use .on with an ancestor scope
But that would require you to know too much about the page being called, IMHO
If the page loaded does silly stuff like document.write and the jQuery parses the script, then you are screwed anyway...
My point is that if a chunk of HTML requires that the caller hookup its own handlers, you'd have to recode the wiring everytime you load it. Ideally, you write a wrapper that takes care of both... but some users may prefer to just call $.load and have it just work.
2

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.");

8 Comments

document.write only overwrites the document if it's not currently open. For example, it's open while the HTML is still being parsed (inline scripts). If you call it after the HTML has loaded, it opens a new document
Still not great... Remove or move the for example. It is ok to do document.write while the document is still open
I mean: document.write will overwrite the document if it's not currently open - it's open while the HTML is still being parsed so inline scripts may use document.write. If you call it after the HTML has loaded, it opens a new document in the window is is called
And opening a new document will override the current one... In all these years as a developer, I NEVER found a use for document.write.
document.write is great for loading external files/resources, based on certain environment conditions
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.