Well, this is weird, but so simple, when I tried to load before my page changing some content of the main index I noticed that something was going wrong because it showed only the actual year "2017", and now I can confirm it because, creating two files test1.html and test2.html this happened:
Test1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$(document).ready(function() {
$("#test").load("test2.html");
});
</script>
</head>
<body>
<div id="test"></div>
</body>
</html>
Test2.html
aaaaaaaaaaaaaa<br>
<script>document.write(new Date().getFullYear())</script>
Result:
So, what can I do to avoid this and show everything without any worries? Trying to make a separate function doesn't work neither...
EDIT:
Now, somebody told me that using document.write is the problem, my problem is that is a simple solution, declaring a html element to write later its content is a little bit less simple and heavier, so, my actual question is know if there is any lightweight replacement for document.write?
I hardcoded my own solution to make everything simplier, I create an element with a data attribute called data-func
, and later I done this:
$(document).ready(function() {
var fncs = $('[data-func]');
fncs.each(function () {
$(this).text(window[$(this).data("func")]);
});
});
The function:
function getYear() {
return new Date().getFullYear();
}
And the element:
<span data-func="getYear"></span>
And thats all!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.