I’m having to use WordPress for this particular page, and I wouldn’t put it past WordPress to have a bug here, but maybe it is just the JavaScript. Why does this compile:
var pData = jQuery.parseJSON(pData);
var strInnerHTML = "";
/*
for (var i = 0; true; i++) {
var obj = pData[i.toString()];
if (obj) {
strInnerHTML += '<option value="' + obj.key + '">' + obj.value + '</option>n';
} else {
break;
}
}
alert(strInnerHTML.slice(0, strInnerHTML.length - 2));
document.getElementById("selectNations").innerHTML = strInnerHTML.slice(0, strInnerHTML.length - 2);*/
and yet this doesn’t:
var pData = jQuery.parseJSON(pData);
var strInnerHTML = "";
/*for (var i = 0; true; i++) {
var obj = pData[i.toString()];
if (obj) {
strInnerHTML += '<option value="' + obj.key + '">' + obj.value + '</option>n';
} else {
break;
}
}
alert(strInnerHTML.slice(0, strInnerHTML.length - 2));
document.getElementById("selectNations").innerHTML = strInnerHTML.slice(0, strInnerHTML.length - 2);*/
EDIT
This is the first example in its full form. In the first example, I’m able to see alert("spam");
getting executed when I load the page, but not in the second example:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript">
// A text file had to be used, since WordPress wouldn't let me upload a JSON file.
alert("spam");
jQuery.get("someJSONFileInTheFormOfATextFile", function(pData) {
var pData = jQuery.parseJSON(pData);
var strInnerHTML = "";
/*
for (var i = 0; true; i++) {
var obj = pData[i.toString()];
if (obj) {
strInnerHTML += '<option value="' + obj.key + '">' + obj.value + '</option>n';
} else {
break;
}
}
alert(strInnerHTML.slice(0, strInnerHTML.length - 2));
document.getElementById("selectNations").innerHTML = strInnerHTML.slice(0, strInnerHTML.length - 2);*/
});
</script>
Found out what happened. When I first started playing with WordPress the other night, I noticed that its Text/HTML tag on a page’s editing screen tries to find newline characters in your code and add its own code around them behind your back. They think this is “convenient and simple”. Well, this principle apparently extends to JavaScript inside of
script
tags. If you do this in JavaScript:then WordPress will try to insert extra characters on and around that blank line on the finished page without telling you. This will break your JavaScript code. One way to work around it though is to just insert
//
everywhere you plan for a line to be blank:This will get WordPress to more-or-less stay out of your way. In the second example in the question, there was a blank line which was not existent in the first example.
You can disable
autop
in the post editor for all posts/pages so WP doesn’t add paragraph breaks.Use these filters in your theme’s functins.php file to disable for
the_content
orthe_excerpt
, or use both:From http://codex.wordpress.org/Function_Reference/wpautop
Or, do the following, which leaves
autop
enabled globally, but lets you disable it with<!-- noformat on -->
and<!-- noformat off -->
tags in individual posts and pages.After you add the function below to your theme’s
functions.php
file, use can use the two tags<!-- noformat on -->
and<!-- noformat off -->
in your page/post editor, i.e.
Content outside of the two format tags will have autop enabled, as noted.
Add to functions.php of your theme: