简体   繁体   English

使用Javascript获取页面的完整源代码

[英]Getting a page's complete source using Javascript

I'm trying to capture and post all JS errors in a page to a Django view. 我正在尝试捕获并将页面中的所有JS错误发布到Django视图。 I'm doing something like this. 我正在做这样的事情。

<script>
    window.onerror = function(errorMsg, file, lineNumber) {
        post_data = {error: errorMsg, file: file, 
        location: window.location.href, lineNumber: lineNumber,
        ua: navigator.userAgent};
        jQuery.post('/js_errors/', post_data);
    }
</script>

The Question: I'd like to add the actual line as well. 问题:我也想添加实际行。 How do I get the line from the page source, given the line number? 在给定行号的情况下,如何从页面源获取行?

So far, I've tried this (accounting for all kinds of newline characters): 到目前为止,我已经尝试过这个(占各种换行符):

document.getElementsByTagName('html')[0].outerHTML.split(/\r?\n/)[lineNumber];

However, this doesn't give me the correct line number. 但是,这并没有给我正确的行号。 What am I missing here? 我在这里错过了什么?

Not particularly efficient, but should get what you're after (ie doctype and all): 不是特别有效,但应该得到你想要的东西(即doctype和all):

lineNumber = 23;
errorLine = null;
$.post("",function(source) {
    errorLine = source.split(/\r?\n/)[lineNumber];
});

使用jquery方法$('#divid_where_you_want_to_extract).load('url');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM