Get of external page using JavaScript</a></h1> </div> <div class="grid fw-wrap pb8 mb16 bb bc-black-075"> <div class="grid--cell ws-nowrap mr16 mb8" title="2016-01-12 19:07:53Z"> <span class="fc-light mr2">Asked</span> <time itemprop="dateCreated" datetime="2018-05-29T21:07:17.153" class="fromnow">May 29 '18 at 21:07</time> </div> <div class="grid--cell ws-nowrap mr16 mb8"> <span class="fc-light mr2">Active</span> <time class="fromnow" title="2018-06-06T17:39:51.917" datetime="2018-06-06T17:39:51.917">Jun 06 '18 at 17:39</a> </div> <div class="grid--cell ws-nowrap mb8" title="Viewed 4,480 times"> <span class="fc-light mr2">Viewed</span> 4,480 times </div> </div> <div id="mainbar" role="main" aria-label="questions and answers"> <div id="question" class="question" data-questionid="50592957" data-ownerid="1375163" data-score="1"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="50592957"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> <button class="js-bookmark-btn s-btn s-btn__unset c-pointer py4"> <svg aria-hidden="true" class="svg-icon iconBookmark" width="18" height="18" viewBox="0 0 18 18"><path d="M6 1a2 2 0 00-2 2v14l5-4 5 4V3a2 2 0 00-2-2H6zm3.9 3.83h2.9l-2.35 1.7.9 2.77L9 7.59l-2.35 1.7.9-2.76-2.35-1.7h2.9L9 2.06l.9 2.77z"></path></svg> <div class="js-bookmark-count mt4" data-value="0">0</div> </button> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>I would like to get the content of the <code><title></code> tag from any specified external page, using JavaScript. Specifically, this is using the <a class="external-link" href="https://zapier.com/help/code/" rel="nofollow noreferrer">Code app by Zapier</a> (vanilla node.js v4.3.2), so additional libraries may not be supported.</p> <p><code>fetch</code> is <a class="external-link" href="https://zapier.com/help/code-examples/#introductory-http-example" rel="nofollow noreferrer">supported</a>...</p> <pre><code>fetch('http://example.com/') .then(function(res) { return res.text(); }) .then(function(body) { var output = {id: 1234, rawHTML: body}; callback(null, output); }) .catch(callback); </code></pre> <p>Docs state: "Very important - be sure to use callback in asynchronous examples!"</p> <p>I am learning JavaScript and have been searching and trying various methods for hours. I don't fully understand the <em>two</em> functions in the example - I only need to return a "title", not the full body.</p> <p>I was using an <a class="external-link" href="https://textance.herokuapp.com/" rel="nofollow noreferrer">API designed to get page titles</a>, but it seems to be a bit flaky. So I am hoping I can get titles using plain code.</p></div> <div class="mt24 mb12"> <div class="post-taglist grid gs4 gsy fd-column"> <div class="grid ps-relative"> <a href="../../questions/tagged/javascript" class="post-tag js-gps-track" title="show questions tagged 'javascript'" rel="tag">javascript</a> <a href="../../questions/tagged/parsing" class="post-tag js-gps-track" title="show questions tagged 'parsing'" rel="tag">parsing</a> <a href="../../questions/tagged/zapier" class="post-tag js-gps-track" title="show questions tagged 'zapier'" rel="tag">zapier</a> </div> </div> </div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="edited Jun 06 '18 at 17:39">edited Jun 06 '18 at 17:39</time> <a href="../../users/4370109/brian-tompsett-tang-lai-en" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/4370109.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Brian Tompsett - 汤莱恩" /> </a> <div class="s-user-card--info"> <a href="../../users/4370109/brian-tompsett-tang-lai-en" class="s-user-card--link">Brian Tompsett - 汤莱恩</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">5,753</li> <li class="s-award-bling s-award-bling__gold" title="72 gold badges">72</li> <li class="s-award-bling s-award-bling__silver" title="57 silver badges">57</li> <li class="s-award-bling s-award-bling__bronze" title="129 bronze badges">129</li> </ul> </div> </div> </div> <div class="post-signature owner grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked May 29 '18 at 21:07">asked May 29 '18 at 21:07</time> <a href="../../users/1375163/robert-andrews" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1375163.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Robert Andrews" /> </a> <div class="s-user-card--info"> <a href="../../users/1375163/robert-andrews" class="s-user-card--link">Robert Andrews</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1,209</li> <li class="s-award-bling s-award-bling__gold" title="4 gold badges">4</li> <li class="s-award-bling s-award-bling__silver" title="23 silver badges">23</li> <li class="s-award-bling s-award-bling__bronze" title="47 bronze badges">47</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-50592957" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="50592957" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-88195991" class="comment js-comment " data-comment-id="88195991" data-comment-owner-id="218196" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment88195991_50592957"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">What is the context? Is this supposed to run in a browser or on the server?</span> – <a href="../../users/218196/felix-kling" title="795,719 reputation" class="comment-user ">Felix Kling</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/50592957/get-title-of-external-page-using-javascript#comment88195991_50592957"><span title="2018-05-29T21:38:51.870 License: CC BY-SA 4.0" class="relativetime-clean">May 29 '18 at 21:38</span></a></span> </div> </div> </li> <li id="comment-88196134" class="comment js-comment " data-comment-id="88196134" data-comment-owner-id="1375163" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment88196134_50592957"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@FelixKling On the server, not browser.</span> – <a href="../../users/1375163/robert-andrews" title="1,209 reputation" class="comment-user owner">Robert Andrews</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/50592957/get-title-of-external-page-using-javascript#comment88196134_50592957"><span title="2018-05-29T21:45:06.320 License: CC BY-SA 4.0" class="relativetime-clean">May 29 '18 at 21:45</span></a></span> </div> </div> </li> <li id="comment-88277593" class="comment js-comment " data-comment-id="88277593" data-comment-owner-id="78782" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment88277593_50592957"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This question is not a duplicate! It is specifically related to Zapier, which has specific limitations. Mod should read the question fully before marking as duplicate.</span> – <a href="../../users/78782/adam-harte" title="10,369 reputation" class="comment-user ">Adam Harte</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/50592957/get-title-of-external-page-using-javascript#comment88277593_50592957"><span title="2018-05-31T22:32:54.503 License: CC BY-SA 4.0" class="relativetime-clean">May 31 '18 at 22:32</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <div id="answers"> <a name="tab-top"></a> <div id="answers-header"> <div class="answers-subheader grid ai-center mb8"> <div class="grid--cell fl1"> <h2 class="mb0" data-answercount="9">1 Answers<span style="display:none;" itemprop="answerCount">1</span></h2> </div> </div> </div> <a name="50593486"></a> <div id="answer-50593486" class="answer " data-answerid="50593486" data-ownerid="78782" data-score="1" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="50593486"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>If you are using Node JS then you can use <code>Request</code> to get the page, then use <a class="external-link" href="https://github.com/cheeriojs/cheerio" rel="nofollow noreferrer">Cheerio</a> to parse its contents. To get the title you might do something like this:</p> <pre><code>const cheerio = require('cheerio'); request('http://example.com/', function (error, response, body) { if (error) { console.log(error); return } var $ = cheerio.load(body); var title = $("title").text(); }); </code></pre> <p>If Cheerio is not available, you could do a more lowtech solution, and just use some simple splits. Not very robust, but might get you what you want.</p> <pre><code>fetch('http://example.com/') .then(function(res) { var body = res.text(); var title = body.split('<title>')[1].split('</title>')[0] }) .catch(callback); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2018-05-29T21:59:58.520" class="relativetime">May 29 '18 at 21:59</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">Adam Harte</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered May 29 '18 at 21:51">answered May 29 '18 at 21:51</time> <a href="../../users/78782/adam-harte" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/78782.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Adam Harte" /> </a> <div class="s-user-card--info"> <a href="../../users/78782/adam-harte" class="s-user-card--link">Adam Harte</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">10,369</li> <li class="s-award-bling s-award-bling__gold" title="7 gold badges">7</li> <li class="s-award-bling s-award-bling__silver" title="52 silver badges">52</li> <li class="s-award-bling s-award-bling__bronze" title="85 bronze badges">85</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-50593486" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="50593486" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-88203938" class="comment js-comment " data-comment-id="88203938" data-comment-owner-id="1375163" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment88203938_50593486"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">`TypeError: body.split is not a function`</span> – <a href="../../users/1375163/robert-andrews" title="1,209 reputation" class="comment-user owner">Robert Andrews</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/50592957/get-title-of-external-page-using-javascript#comment88203938_50593486"><span title="2018-05-30T06:10:01.563 License: CC BY-SA 4.0" class="relativetime-clean">May 30 '18 at 06:10</span></a></span> </div> </div> </li> <li id="comment-88207814" class="comment js-comment " data-comment-id="88207814" data-comment-owner-id="1375163" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment88207814_50593486"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Cheerio not available. Regular method produces the above error, which I take to refer to string definition. But I can't seem to successfully use toString() to fix, which is a solution I read about. Any ideas?</span> – <a href="../../users/1375163/robert-andrews" title="1,209 reputation" class="comment-user owner">Robert Andrews</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/50592957/get-title-of-external-page-using-javascript#comment88207814_50593486"><span title="2018-05-30T08:01:42.697 License: CC BY-SA 4.0" class="relativetime-clean">May 30 '18 at 08:01</span></a></span> </div> </div> </li> <li id="comment-88238532" class="comment js-comment " data-comment-id="88238532" data-comment-owner-id="1375163" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment88238532_50593486"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Got it working, I think - https://pastebin.com/BEyj0WSm</span> – <a href="../../users/1375163/robert-andrews" title="1,209 reputation" class="comment-user owner">Robert Andrews</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/50592957/get-title-of-external-page-using-javascript#comment88238532_50593486"><span title="2018-05-30T22:52:54.883 License: CC BY-SA 4.0" class="relativetime-clean">May 30 '18 at 22:52</span></a></span> </div> </div> </li> <li id="comment-88239174" class="comment js-comment " data-comment-id="88239174" data-comment-owner-id="78782" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment88239174_50593486"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Your code looks functionally identical to mine. Strange!</span> – <a href="../../users/78782/adam-harte" title="10,369 reputation" class="comment-user ">Adam Harte</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/50592957/get-title-of-external-page-using-javascript#comment88239174_50593486"><span title="2018-05-30T23:34:39.303 License: CC BY-SA 4.0" class="relativetime-clean">May 30 '18 at 23:34</span></a></span> </div> </div> </li> <li id="comment-118560677" class="comment js-comment " data-comment-id="118560677" data-comment-owner-id="4482748" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment118560677_50593486"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">what happens if the <title> tag has an attribute? for example: `<title data-react-helmet="true">` – Csaba Apr 13 '21 at 12:42