我将如何更换元标题,关键字和描述的AJAX加载的html文件? [英] How would I replace the meta title, keywords and description on the ajax loaded html document?

查看:179
本文介绍了我将如何更换元标题,关键字和描述的AJAX加载的html文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将取代元标题和描述上与Ajax调用??

元标题和描述从传入的HTML文档的文档

我打电话从#内容DIV上相邻的html文件的新内容。当新的内容负载我想更换主文件标题我希望发生的是(与描述和关键字......可能因为即时通讯工具上最多有反正以及)与正在加载的HTML文件的元所有权

我也想过用替换();或匹配();但我只是想找到最好的出路。

下面是脚本:

  // JavaScript的文档
$(文件)。就绪(函数(){

    VAR toLoad
    $(窗口).bind(hashchange功能(E){
            loadcontent();
            返回false;

    });



        $('#TOC李一)。点击(函数(){
            。window.location.hash = $(本).attr(HREF)SUBSTR(0,$(本).attr(HREF),长度为0);
            VAR HREF = $(本).attr(HREF);
            $('元[名称=标题]')ATTR(标题,new_title)。
            $('元[名称=介绍]')ATTR(说明,new_description)。
            $('元[名称=关键字]')ATTR('关键字',new_keywords)。
            //警报(哎+ window.location.hash);
            $('#面包屑H1')附加(&所述; / a取代;&所述; A HREF =。index.html的+ window.location.hash +'>中+ $(本).attr('的href' )+>&所述; / A>中);
            $ .bbq.pushState({网址:HREF});
            $(窗口).trigger(hashchange);

        });

    loadcontent();

});

功能loadcontent(){

    变种toLoad = window.location.hash.replace(#,)+的HTML#内容。';
            $('#内容),隐藏(慢);
            $('#负荷)删除()。
            $('#conContainer)追加('<跨度ID =负荷> LOADING ...< / SPAN>');
            $('#负荷)淡入(正常)。

            $('#内容)。载荷(toLoad,'',功能(returnText,状态,请求){
                showNewContent()
            });

            功能loadContent(){
                $('#内容)的负载(toLoad,'',showNewContent());

            }
            功能showNewContent(){
                $('#内容)显示(慢,hideLoader());
            }
            功能hideLoader(){
                $('#负荷)淡出(正常)。
            }

}
 

解决方案

有关,你可以做以下标题:

  $(标题)HTML(我的新的元标题')。
 

给你的元描述和关键字元素的ID,这样你就可以轻松地选择元素:

  $('#mdescription)ATTR(内容,我的新的meta描述)。
$('#mdkeywords)ATTR('内容','关键字之一,关键字两个)。
 

How would I replace the meta title and description on the document with the meta title and description from an incoming html document on an ajax call??

I am calling new content from the #content div on an adjacent html document. What I want to happen is when the new content loads I want to replace the main documents title (and description and keywords... might as well since im tooling around up there anyway) with the meta title in the html file that is being loaded.

I have thought about using replace(); or match(); but I just wanted to find out the best way.

here is the script:

// 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');
            }

}

解决方案

For the title you could do the following:

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

Give your meta description and keywords elements an id, so you can easily select the elements:

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

这篇关于我将如何更换元标题,关键字和描述的AJAX加载的html文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆