最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get the page's full content as string in javascript? - Stack Overflow

programmeradmin2浏览0评论

I'm writing a bookmarklet, i.e. a bookmark that contains javascript instead of a URL, and I have some trouble. In fact, I cannot remember how I can get the content of the page as a string, so I can apply a regular expression to find what I want. Can you please help me on this?

Before anyone suggests it, I cannot use getElementBy(Id/Name/Tag), because the data I'm looking for is HTML-mented and inside markups, so I don't think that would work.

Thanks.

I'm writing a bookmarklet, i.e. a bookmark that contains javascript instead of a URL, and I have some trouble. In fact, I cannot remember how I can get the content of the page as a string, so I can apply a regular expression to find what I want. Can you please help me on this?

Before anyone suggests it, I cannot use getElementBy(Id/Name/Tag), because the data I'm looking for is HTML-mented and inside markups, so I don't think that would work.

Thanks.

Share Improve this question asked Feb 5, 2009 at 17:33 AntoineAntoine 5,26512 gold badges56 silver badges88 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

You can access it through:

document.body.innerHTML

so I can apply a regular expression to find what I want

Do. Not. Use. Regex. To. Parse. HTML.

Especially when the browser has already parsed it for you! Come ON!

the data I'm looking for is HTML-mented

You can perfectly well grab ment content out of the DOM. eg.

<div id="mything"><!-- la la la I'm a big ment --></div>

alert(document.getElementById('mything').firstChild.data);

And if you need to search the DOM for ment elements:

// Get ment descendents
//
function dom_getComments(parent, recurse) {
    var results= [];
    for (var childi= 0; childi<parent.childNodes.length; childi++) {
        var child= parent.childNodes[childi];
        if (child.nodeType==8) // Node.COMMENT_NODE
            results.push(child);
        else if (recurse && child.nodeType==1) // Node.ELEMENT_NODE
            results= results.concat(dom_getComments(child));
    }
    return results;
}
发布评论

评论列表(0)

  1. 暂无评论