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

javascript - Using Document object in nodejs - Stack Overflow

programmeradmin1浏览0评论

I'm still new to javascript and node.js and was trying to acplish something but I am encountering the error ReferenceError: Document not defined

Given the following code in seperate files:

Index.html:

<!DOCTYPE html>
<html>
<head>
    <title>My Test Website</title>
    <script type="text/javascript" src="script.js"></script>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <p id="para1"> Some text here</p>
</body>
</html>

And script.js:

function getParagraphContents() {
    var paragraph = document.getElementById("para1");
    var temp = paragraph.textContent;
    console.log(temp);
}

So my problem being is I wanted to be able to run the mand node script.js from the mand line but keep getting an error message. I keep reading other answers and documentation but nothing seems to be the right answer. Any suggestions? P.S. I am using terminal on mac osx and have node and npm install correctly.

Thank you.

I'm still new to javascript and node.js and was trying to acplish something but I am encountering the error ReferenceError: Document not defined

Given the following code in seperate files:

Index.html:

<!DOCTYPE html>
<html>
<head>
    <title>My Test Website</title>
    <script type="text/javascript" src="script.js"></script>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <p id="para1"> Some text here</p>
</body>
</html>

And script.js:

function getParagraphContents() {
    var paragraph = document.getElementById("para1");
    var temp = paragraph.textContent;
    console.log(temp);
}

So my problem being is I wanted to be able to run the mand node script.js from the mand line but keep getting an error message. I keep reading other answers and documentation but nothing seems to be the right answer. Any suggestions? P.S. I am using terminal on mac osx and have node and npm install correctly.

Thank you.

Share Improve this question asked Aug 3, 2016 at 16:23 Christian ValdezChristian Valdez 531 gold badge1 silver badge5 bronze badges 3
  • Why do you need that ? – Ismail RBOUH Commented Aug 3, 2016 at 16:26
  • 1 You can't run that clientside script in Node.js, that's not how it works, that script is for the browser, not the server. – adeneo Commented Aug 3, 2016 at 16:27
  • script.js is designed to run in a browser and to operate on the current page's HTML. Running node script.js does not really make any sense. While you can find libraries that will load an HTML document into node and give you a DOM representation of them that you can then run some browser-like scripts on, that isn't what you're attempting here as there is no document associated with script.js at all. It's just a script by itself. So running node script.js is like half an app. You need to explain what you're trying to acplish from node. – jfriend00 Commented Aug 3, 2016 at 16:46
Add a ment  | 

1 Answer 1

Reset to default 7

script.js is a script designed to run in a browser, not in node.js. It is designed to operate on the current web page and uses the document object to get access to that page.

A node.js execution environment is not the same as a browser. For example, there is no notion of the "current page" like there is in a browser and thus there is no global document or window object either like there is in a browser.

So, this all explains why trying to run node script.js doesn't work. There's no global document object so your script quickly generates an error and exits.


We can't really tell from your question if you just don't really understand the difference between running scripts in a browser vs. running scripts in a node.js environment and that's all you need to have explained or if you actually want to operate on an HTML document from within node.js?

It is possible to operate on HTML pages from within node.js. The usual way to do that is to get an HTML parsing library like jsdom. You can then load some HTML page using that library and it will create a simulated browser DOM and will make things like the document object available to you so you could execute a browser-like script on an HTML document.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>