【问题标题】:How to add additional functionalities?如何添加额外的功能?
【发布时间】:2015-07-23 10:47:42
【问题描述】:

我有一个简单的基于 JavaScript 的博客。首先看看下面的代码,我会问我的问题。

Index.html 正文中包含以下代码

<script language="javascript" type="text/javascript" src="blog/config.js"> </script>
<script language="javascript" type="text/javascript" src="blog/single.js"> </script>
<script language="javascript" type="text/javascript" src="blog/posts.js"> </script>

config.js 有

//This is the configuration file of the blog system.
//change these variables to suit your style and needs 
var head = "h2"; //the heading style, ex. h1, h2, ect. use "h2" rather than "<h2>"
var text = "text"; //the text style, from your style sheet, it's in a <div> tag
var divider = "<hr>"; //the division between posts
var newer = "newer"; //the class for the link to the next newest page
var older = "older"; //the class for the link to the next oldest page
var pageclass = "page"; //the class for the text that displays the page number
var dateclass = "date"; //the class for the date
var pagesize = 4; //the number of posts on each page
var navclass = nav; //the configuration for the navigation`

posts.js

var posts = 1; //add 1 to this after adding a post. should be equal to the id of the newest post.
initblog(posts);
var id = 1; //make sure that this number is one greater than the one below it
var date = "mm/dd/yyyy"; //The date of the post
var heading = "Post 1"; //The title
var entry = ""; //reset the string
//don't worry about formatting and stuff like that, the system takes care of it all for us.
//VV your entry VV
entry += "<p>Wow, this post is on another page, If you have this many real posts, congratulations!</p>";
//^^ The most important part ^^
add_entry(id,date,heading,entry); //adds the entry to the blog

single.js

var maxpost;
function initblog(posts){
    maxpost = posts;
    var address = window.location.search;
    if (address.substring(0, 1) == '?') {
        page = address.substring(1);
    } else{
        window.location = "post.html?" + posts;
    }

    page = parseInt(page);
    if (page > maxpost){
        page = maxpost;
    }

    if (page < 1){
        page = 1;
    }   

}
function add_entry(id,date,heading,entry) {
for (i=page;i>page - 1;i--){
    if (id == i){
        var entrytext = "";
        entrytext += "<div class=" + text + ">";    
        entrytext += "<" + head + ">";
        entrytext += "<a name=" + id + "></a>";
        entrytext += "<span class='date'>[" + date + "]</span> ";
        entrytext += heading;
        entrytext += "</" + head + ">";    
        entrytext += entry;
        entrytext += "</div>" + divider;
        document.write(entrytext);
    }
}
}

 function pages(){
entrytext = ""
entrytext += "<table class=\"nav\"><tr>";
entrytext += "<td width=25% class = " + newer + ">&nbsp";
if (page < maxpost){
entrytext += "<A HREF=javascript:prev()>Newer Posts </A>";
}
entrytext += "</td><td width=50% class = " + pageclass + "><br><A HREF=javascript:newest()> Back to Index</A></td>"; 
entrytext += "<td width=25% class = " + older + ">&nbsp";
if (page-1 > 0){
    entrytext += "<A HREF=javascript:next()>Older Posts</A>";
}
entrytext += "</td></table>";
entrytext += "";
document.write(entrytext);
}

function next(){
page = page - 1;
if (page < 1) {
    page = page + 1;
}
window.location = "post.html?" + page;
}

function prev(){
page = page + 1;
if (page > maxpost) {
    page = maxpost;
}
window.location = "post.html?" + page;
 }

function newest(){
window.location = "index.html?" + maxpost;
 }

嗯,这就是整个博客脚本。我没有添加样式,为简单起见,您可能会在每行上看到 cmets。 此博客没有添加标题和元描述、关键字等选项。由于应用它的风格,它不能在正文标签之外做任何事情。

1 .如何添加获取/加载标题的选项? 2.如何添加加载元标记的功能?

不要告诉我在模板 (index.HTML) 上编辑和添加标题,因为那没有意义

【问题讨论】:

  • 什么样的元数据?
  • 您所说的“获取/加载”标题是什么意思?你有“标题”属性。不解决你的问题?
  • 抱歉不是元数据,它们是元标签(
  • 这是一个博客脚本,所以每个帖子都应该有自己的标题和元标记。但在这里,它还没有

标签: javascript html javascript-objects


【解决方案1】:

如您所见,标题块是博客的标题。你所需要的只是让它更显眼。

    var entrytext = "";
    entrytext += "<div class=" + text + ">";    
    entrytext += "<h1>" + heading + "</h1>";
    entrytext += "<" + head + ">";
    entrytext += "<a name=" + id + "></a>";
    entrytext += "<span class='date'>[" + date + "]</span> ";
    entrytext += "</" + head + ">";    
    entrytext += entry;
    entrytext += "</div>" + divider;
    document.write(entrytext);
    document.title = heading;

这将解决您关于标题的问题。

关于元标记,通常(实际上按照标准)元标记写在 HTML 中的

标记之间。要使其符合 SEO,您需要将它们添加到这些标签中。更详细:http://www.w3schools.com/tags/tag_meta.asp

但是,如果此代码是在客户端生成的。生成它没有任何意义,因为搜索引擎不会解析即时生成的元标记。因为它是在浏览器上执行的。

【讨论】:

  • 你不明白,我想说什么。标题是指 标签内的文本(<title>This is a sample Title ),会出现在浏览器的 URL 框中。一切都应该在 SE0 优化的 head 标签中。如果您愿意帮助我,我愿意提供资源。
  • 在这种情况下添加 document.title = 标题行
  • 对不起,我不明白。什么意思?
  • 知道了。现在看起来很酷。但仍然需要您注意元标记。有什么想法吗?
  • 嘿,我找到了一些代码。 document.head.innerHTML = "&lt;meta name=" description" content="this is a sample desc"&gt;"; 它也有效。但应用它后,我的网站结构正在发生变化。你知道为什么吗?
猜你喜欢
  • 1970-01-01
  • 2017-03-10
  • 2022-12-28
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多