I don’t know whether that’s a good thread title or not, but we’ll go with it anyway!
Ok, so I’m slightly confused regarding the dynamic nature of JavaScript and how it outputs content in the HTML markup source code.
If we want to add any dynamic content to a Web page with PHP (in PHP tags obviously) then we can. If we view the source of the Web page then we’ll see the content within the markup, whether the PHP is inline or has been written to the page through a templating system.
However, I’ve had a look around the Internet and done a few little experiments myself and have come to the conclusion that any content coded within JavaScript won’t appear in the actual source code of the HTML markup / page. A simple example is below:
date.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Date</title>
<script type="text/javascript" src="date.js"></script>
</head>
<body>
<div>
<p id="dateField"></p>
</div>
</body>
</html>
date.js
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
function currentDate() {
var dayName = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var monName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var now = new Date();
var dateString = dayName[now.getDay()] + ", " + monName[now.getMonth()] + " " + now.getDate() + ", " + now.getFullYear();
document.getElementById("dateField").innerHTML = dateString;
}
addLoadEvent(currentDate);
Now, if you load up date.html in the Web browser and view the source code everything appears as normal however, nothing appears within the
<p id="dateField"></p>
tags. If you had a similar date script written with PHP then it would appear in the view source code.
So I guess my question is - Can someone explain to me exactly HOW dynamic JavaScript is?
What content will appear in the markup source code of a document and what won’t? Is there a way to have the content within a JS script appear in the source code as it usually would?
If the content within the JS code doesn’t exist in the actual source code after the Web page has been fully loaded, does that mean that the said content won’t appear to search engines? Doesn’t this mean that, like Flash content, the content within a JS script is not search engine friendly?
Hope I can get some clarification and help on this. Thanks.
Andrew Cooper