Javascript在< head>中抓取Javascript评论 [英] Javascript to grab Javascript comments within <head>

查看:120
本文介绍了Javascript在< head>中抓取Javascript评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页开头如下:

<html>
        <head>
        <!-- 12036,2011-11-29/11:02 -->
        <title>Products & Services</title>

我想在这里抓取12036号码并在底部绝对定位的DIV中显示我的屏幕右侧。我的目的是要么将它用作书签或Greasemonkey脚本。

I would like to grab the 12036 number here and to display it within an absolutely positioned DIV at the bottom right of my screen. My intention here is to either to use it as a bookmarklet or as a Greasemonkey script.

这样做的目的是该数字代表我们在内部使用引用的PAGE ID。

The purpose of this is that the number represents a PAGE ID that we use reference internally.

我开始使用:

var x = document.getElementsByTagName('head').innerHTML;

但我无法继续前进。

任何人都可以在这里协助吗?

Can anyone assist here?

推荐答案

这将在最新浏览器,但你需要修改它以适应旧的浏览器......

This will work in latest browsers but you will need to modify it to suit older browsers...

var commentNode = [].slice.call(document.head.childNodes).filter(function(node) {
            return node.nodeType == 8;
          })[0],
    id = commentNode.data.match(/^\s*(\d+)/)[1];

将它定位在屏幕的右下角......

To position it at the bottom corner of your screen...

var elem = document.createElement('div');

elem.id = 'id-display';

elem.appendChild(document.createTextNode(id));

document.body.appendChild(elem);

...还有一点CSS ......

...and a touch of CSS...

#id-display {
    position: fixed;
    bottom: 0;
    right: 0;
    background: #333;
    color: #fff;
    padding: 6px;
}

JSBin

这篇关于Javascript在&lt; head&gt;中抓取Javascript评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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