【问题标题】:How would I replace the meta title, keywords and description on the ajax loaded html document?如何替换 ajax 加载的 html 文档上的元标题、关键字和描述?
【发布时间】:2012-06-10 18:40:33
【问题描述】:

如何将文档上的元标题和描述替换为 ajax 调用中传入的 html 文档的元标题和描述??

我正在从相邻 html 文档的#content div 调用新内容。我想要发生的是,当新内容加载时,我想用正在加载的 html 文件中的元标题替换主文档标题(以及描述和关键字......也可能因为我在那里使用工具) .

我考虑过使用 replace();或匹配();但我只是想找出最好的方法。

这是脚本:

// JavaScript Document
$(document).ready(function() {

    var toLoad
    $(window).bind( "hashchange", function(e) {
            loadcontent();
            return false;

    });



        $('#toc li a').click(function(){
            window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-0);
            var href = $(this).attr( "href" );
            $('meta[name=title]').attr('title', new_title);
            $('meta[name=description]').attr('description', new_description);
            $('meta[name=keywords]').attr('keywords', new_keywords);
            //alert("hey" + window.location.hash);
            $('#breadcrumbs h1').append(" </a><a href='index.html"+ window.location.hash + "' >" + $(this).attr('href') +" ></a>");
            $.bbq.pushState({ url: href });
            $(window).trigger( "hashchange" );

        });

    loadcontent();

});

function loadcontent(){

    var toLoad = window.location.hash.replace("#","") +'.html #content';
            $('#content').hide('slow');
            $('#load').remove();
            $('#conContainer').append('<span id="load">LOADING...</span>');
            $('#load').fadeIn('normal');

            $('#content').load(toLoad,'',function(returnText,status,request){
                showNewContent()
            });

            function loadContent() {
                $('#content').load(toLoad,'',showNewContent());

            }
            function showNewContent() {
                $('#content').show('slow',hideLoader());
            }
            function hideLoader() {
                $('#load').fadeOut('normal');
            }

}

【问题讨论】:

  • 它是一个内部搜索引擎,将在一个巨大的网站上使用......我想这样做只是为了掩盖我的踪迹
  • 根据我已阅读的文档,只要正在加载的 html 文件具有 doctype 并设置为自己的 url 后备,谷歌就会通过哈希标签索引 ajax 加载的内容
  • 是的,但作为后备,您需要使您的页面无需 javascript 即可访问。
  • 如何从 window.location 中删除 .html??

标签: jquery ajax replace title meta-tags


【解决方案1】:

对于标题,您可以执行以下操作:

$('title').html('my new meta title');

为您的元描述和关键字元素提供一个 ID,以便您轻松选择元素:

$('#mdescription').attr('content', 'my new meta description');
$('#mdkeywords').attr('content', 'keyword one, keyword two');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多