document.write 相关问题

DOM Level 2 HTML方法,它将文本字符串写入document.open()打开的文档流。

动态添加带有src的脚本标签,可能包含document.write

我想在网页中动态包含脚本标签,但是我无法控制它的 src,所以 src="source.js" 可能看起来像这样。 文档.write(' <p>我想在网页中动态包含脚本标签,但是我无法控制它的 src,所以 src="source.js" 可能看起来像这样。</p> <pre><code>document.write(&#39;&lt;script type=&#34;text/javascript&#34;&gt;&#39;) document.write(&#39;alert(&#34;hello world&#34;)&#39;) document.write(&#39;&lt;/script&gt;&#39;) document.write(&#39;&lt;p&gt;goodbye world&lt;/p&gt;&#39;) </code></pre> <p>现在平常放</p> <pre><code>&lt;script type=&#34;text/javascript&#34; src=&#34;source.js&#34;&gt;&lt;/script&gt; </code></pre> <p>在头部工作正常,但是有没有其他方法可以使用 <pre><code>source.js</code></pre> 之类的东西动态添加 <pre><code>innerHTML</code></pre>?</p> <p><a href="http://jsfiddle.net/PRNnm/" rel="noreferrer">jsfiddle我所尝试过的</a></p> </question> <answer tick="false" vote="323"> <pre><code>var my_awesome_script = document.createElement(&#39;script&#39;); my_awesome_script.setAttribute(&#39;src&#39;,&#39;http://example.com/site.js&#39;); document.head.appendChild(my_awesome_script); </code></pre> </answer> <answer tick="false" vote="130"> <p>您可以像这样使用 <a href="https://developer.mozilla.org/en-US/docs/DOM/document.createElement" rel="noreferrer"><pre><code>document.createElement()</code></pre></a> 功能:</p> <pre><code>function addScript( src ) { var s = document.createElement( &#39;script&#39; ); s.setAttribute( &#39;src&#39;, src ); document.body.appendChild( s ); } </code></pre> </answer> <answer tick="false" vote="98"> <p>有<pre><code>onload</code></pre>函数,当脚本加载成功时可以调用它:</p> <pre><code>function addScript( src, callback ) { var s = document.createElement( &#39;script&#39; ); s.setAttribute( &#39;src&#39;, src ); s.onload=callback; document.body.appendChild( s ); } </code></pre> </answer> <answer tick="false" vote="39"> <p>差不多十年过去了,没有人费心去写 <pre><code>Promise</code></pre> 版本,所以这是我的(基于 <a href="https://stackoverflow.com/a/13122014/9449426">this</a> 答案):</p> <pre><code>const addScript = async src =&gt; new Promise((resolve, reject) =&gt; { const el = document.createElement(&#39;script&#39;); el.src = src; el.addEventListener(&#39;load&#39;, resolve); el.addEventListener(&#39;error&#39;, reject); document.body.append(el); }); </code></pre> <h2>使用方法</h2> <pre><code>try { await addScript(&#39;https://api.stackexchange.com/js/2.0/all.js&#39;); // do something after it was loaded } catch (e) { console.log(e); } </code></pre> </answer> <answer tick="false" vote="21"> <p>我编写的一个很好的小脚本来加载多个脚本:</p> <pre><code>function scriptLoader(scripts, callback) { var count = scripts.length; function urlCallback(url) { return function () { console.log(url + &#39; was loaded (&#39; + --count + &#39; more scripts remaining).&#39;); if (count &lt; 1) { callback(); } }; } function loadScript(url) { var s = document.createElement(&#39;script&#39;); s.setAttribute(&#39;src&#39;, url); s.onload = urlCallback(url); document.head.appendChild(s); } for (var script of scripts) { loadScript(script); } }; </code></pre> <p>用途:</p> <pre><code>scriptLoader([&#39;a.js&#39;,&#39;b.js&#39;], function() { // use code from a.js or b.js }); </code></pre> </answer> <answer tick="false" vote="18"> <p>当脚本异步加载时,它们无法调用 document.write。这些调用将被忽略,并且警告将写入控制台。</p> <p>您可以使用以下代码动态加载脚本:</p> <pre><code>var scriptElm = document.createElement(&#39;script&#39;); scriptElm.src = &#39;source.js&#39;; document.body.appendChild(scriptElm); </code></pre> <p>仅当您的源属于单独的文件时,此方法才有效。</p> <p>但是,如果您有源代码作为内联函数,您想要动态加载并且想要向脚本标记添加其他属性,例如类、类型等,那么以下代码片段将对您有所帮助:</p> <pre><code>var scriptElm = document.createElement(&#39;script&#39;); scriptElm.setAttribute(&#39;class&#39;, &#39;class-name&#39;); var inlineCode = document.createTextNode(&#39;alert(&#34;hello world&#34;)&#39;); scriptElm.appendChild(inlineCode); document.body.appendChild(scriptElm); </code></pre> </answer> <answer tick="false" vote="12"> <p>您可以尝试以下代码片段。</p> <pre><code>function addScript(attribute, text, callback) { var s = document.createElement(&#39;script&#39;); for (var attr in attribute) { s.setAttribute(attr, attribute[attr] ? attribute[attr] : null) } s.innerHTML = text; s.onload = callback; document.body.appendChild(s); } addScript({ src: &#39;https://www.google.com&#39;, type: &#39;text/javascript&#39;, async: null }, &#39;&lt;div&gt;innerHTML&lt;/div&gt;&#39;, function(){}); </code></pre> </answer> <answer tick="false" vote="10"> <p>一句话(不过与上面的答案没有本质区别):</p> <pre><code>document.body.appendChild(document.createElement(&#39;script&#39;)).src = &#39;source.js&#39;; </code></pre> </answer> <answer tick="false" vote="6"> <p><strong>这对我来说是工作。</strong> </p> <p>你可以检查一下。</p> <p></p><div data-babel="false" data-lang="js" data-hide="false" data-console="true"> <div> <pre><code>var script_tag = document.createElement(&#39;script&#39;); script_tag.setAttribute(&#39;src&#39;,&#39;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&#39;); document.head.appendChild(script_tag); window.onload = function() { if (window.jQuery) { // jQuery is loaded alert(&#34;ADD SCRIPT TAG ON HEAD!&#34;); } else { // jQuery is not loaded alert(&#34;DOESN&#39;T ADD SCRIPT TAG ON HEAD&#34;); } }</code></pre> </div> </div> <p></p> </answer> <answer tick="false" vote="4"> <p>以正确的顺序加载相互依赖的脚本。</p> <p>基于 <a href="https://stackoverflow.com/a/57162086/1675736">Satyam Pathak</a> 响应,但修复了加载。 它是在脚本实际加载之前触发的。</p> <p></p><div data-babel="false" data-lang="js" data-hide="false" data-console="true"> <div> <pre><code>const scripts = [&#39;https://www.gstatic.com/firebasejs/6.2.0/firebase-storage.js&#39;, &#39;https://www.gstatic.com/firebasejs/6.2.0/firebase-firestore.js&#39;, &#39;https://www.gstatic.com/firebasejs/6.2.0/firebase-app.js&#39;] let count = 0 const recursivelyAddScript = (script, cb) =&gt; { const el = document.createElement(&#39;script&#39;) el.src = script if(count &lt; scripts.length) { count ++ el.onload = () =&gt; recursivelyAddScript(scripts[count]) document.body.appendChild(el) } else { console.log(&#39;All script loaded&#39;) return } } recursivelyAddScript(scripts[count])</code></pre> </div> </div> <p></p> </answer> <answer tick="false" vote="3"> <p>嗯,有多种方法可以包含动态 JavaScript, 我在很多项目中都使用这个。</p> <pre><code>var script = document.createElement(&#34;script&#34;) script.type = &#34;text/javascript&#34;; //Chrome,Firefox, Opera, Safari 3+ script.onload = function(){ console.log(&#34;Script is loaded&#34;); }; script.src = &#34;file1.js&#34;; document.getElementsByTagName(&#34;head&#34;)[0].appendChild(script); </code></pre> <p>您可以调用创建一个通用函数,它可以帮助您根据需要加载尽可能多的javascript文件。这里有关于此的完整教程。</p> <p><a href="https://movingdouble.com/inserting-dynamic-javascript-the-right-way/" rel="nofollow noreferrer">以正确的方式插入动态Javascript</a></p> </answer> <answer tick="false" vote="3"> <p>没有人提到这一点,但您也可以使用 <pre><code>URL</code></pre> 和 <pre><code>Blob</code></pre> 创建 URL,将实际源代码粘贴到脚本标记中:</p> <pre><code>const jsCode = ` // JS code in here. Maybe you extracted it from some HTML string. ` const url = URL.createObjectURL(new Blob([jsCode])) const script = document.createElement(&#39;script&#39;) script.src = url URL.revokeObjectURL(url) // dispose of it when done </code></pre> <p>至于<pre><code>jsCode</code></pre>,你可能已经从一些HTML中得到了它。</p> <p>这里有一个更完整的示例,说明如何处理 HTML 源中的任意数量的脚本:</p> <pre><code>main() async function main() { const scriptTagOpen = /&lt;script\b[^&gt;]*&gt;/g const scriptTagClose = /&lt;\/script\b[^&gt;]*&gt;/g const scriptTagRegex = /&lt;script\b[^&gt;]*&gt;[\s\S]*?&lt;\/script\b[^&gt;]*&gt;/g const response = await fetch(&#39;path/to/some.html&#39;) const html = await response.text() someElement.innerHTML = html // We need to get the script tags and manually add them to DOM // because otherwise innerHTML will not execute them. const codes = html .match(scriptTagRegex) ?.map(code =&gt; code.replace(scriptTagOpen, &#39;&#39;).replace(scriptTagClose, &#39;&#39;)) .map(code =&gt; URL.createObjectURL(new Blob([code]))) || [] for (const code of codes) { const script = document.createElement(&#39;script&#39;) script.src = code someElement.append(script) URL.revokeObjectURL(code) } } </code></pre> </answer> <answer tick="false" vote="1"> <p>执行此操作的唯一方法是将 <pre><code>document.write</code></pre> 替换为您自己的函数,该函数会将元素附加到页面底部。使用 jQuery 非常简单:</p> <pre><code>document.write = function(htmlToWrite) { $(htmlToWrite).appendTo(&#39;body&#39;); } </code></pre> <p>如果您的 html 像问题示例一样以块形式进入 document.write,则需要缓冲 <pre><code>htmlToWrite</code></pre> 段。也许是这样的:</p> <pre><code>document.write = (function() { var buffer = &#34;&#34;; var timer; return function(htmlPieceToWrite) { buffer += htmlPieceToWrite; clearTimeout(timer); timer = setTimeout(function() { $(buffer).appendTo(&#39;body&#39;); buffer = &#34;&#34;; }, 0) } })() </code></pre> </answer> <answer tick="false" vote="1"> <p>我通过递归附加每个脚本来尝试</p> <p><strong>注意</strong> 如果您的脚本相互依赖,则位置需要同步。</p> <p>主要依赖项应该位于数组的最后,以便初始脚本可以使用它</p> <p></p><div data-babel="false" data-lang="js" data-hide="false" data-console="true"> <div> <pre><code>const scripts = [&#39;https://www.gstatic.com/firebasejs/6.2.0/firebase-storage.js&#39;, &#39;https://www.gstatic.com/firebasejs/6.2.0/firebase-firestore.js&#39;, &#39;https://www.gstatic.com/firebasejs/6.2.0/firebase-app.js&#39;] let count = 0 const recursivelyAddScript = (script, cb) =&gt; { const el = document.createElement(&#39;script&#39;) el.src = script if(count &lt; scripts.length) { count ++ el.onload = recursivelyAddScript(scripts[count]) document.body.appendChild(el) } else { console.log(&#39;All script loaded&#39;) return } } recursivelyAddScript(scripts[count])</code></pre> </div> </div> <p></p> </answer> <answer tick="false" vote="0"> <p>这是一个缩小的片段,与 Google Analytics 和 Facebook Pixel 使用的代码相同:</p> <pre><code>!function(e,s,t){(t=e.createElement(s)).async=!0,t.src=&#34;https://example.com/foo.js&#34;,(e=e.getElementsByTagName(s)[0]).parentNode.insertBefore(t,e)}(document,&#34;script&#34;); </code></pre> <p>将 <pre><code>https://example.com/foo.js</code></pre> 替换为您的脚本路径。</p> </answer> <answer tick="false" vote="-1"> <p></p><div data-babel="false" data-lang="js" data-hide="false" data-console="true"> <div> <pre><code>window.addEventListener(&#34;load&#34;, init); const loadScript = async (url) =&gt; { const response = await fetch(url); const script = await response.text(); eval(script); } function init() { const wistiaVideo = document.querySelector(&#34;.wistia_embed&#34;); if (&#34;IntersectionObserver&#34; in window &amp;&amp; &#34;IntersectionObserverEntry&#34; in window &amp;&amp; &#34;intersectionRatio&#34; in window.IntersectionObserverEntry.prototype) { let lazyVideoObserver = new IntersectionObserver(function (entries, observer) { entries.forEach(function (entry) { if (entry.isIntersecting) { setTimeout(() =&gt; loadScript(&#34;//fast.wistia.com/assets/external/E-v1.js&#34;), 1000); lazyVideoObserver.unobserve(entry.target); console.log(&#34;E-v1.js script loaded from fast.wistia.com&#34;); } }); }); lazyVideoObserver.observe(wistiaVideo); } }</code></pre> <pre><code>&lt;div style=&#34;height: 150vh; background-color: #f7f7f7;&#34;&gt;&lt;/div&gt; &lt;h1&gt;Wistia Video!&lt;/h1&gt; &lt;div class=&#34;wistia_embed wistia_async_29b0fbf547&#34; style=&#34;width:640px;height:360px;&#34;&gt;&amp;nbsp;&lt;/div&gt; &lt;h1&gt;Video Ended!&lt;/h1&gt;</code></pre> </div> </div> <p></p> </answer> </body></html>

回答 0 投票 0

如何使用js错误标签来抛出文件或其他html页面?

我正在制作一个关于 HTML 的教程,并正在尝试制作这样的系统。将出现一个问题,您需要在文本框中输入正确答案。根据你的回答,电脑会显示

回答 1 投票 0

如何将此document.write转换为innerHTML?

我正在使用此脚本来输出帖子总数。 函数 mbhTotalCount(json) { var TotalCount = parseInt(json.feed.openSearch$totalResults.$t, 10); 文档.write (</desc> <question vote="0"> <p>我正在使用此脚本来输出帖子总数。</p> <p></p><div data-babel="false" data-lang="js" data-hide="false" data-console="true"> <div> <pre><code>&lt;script&gt; function mbhTotalCount(json) { var totalCount = parseInt(json.feed.openSearch$totalResults.$t, 10); document.write (totalCount); } &lt;/script&gt; &lt;span class=&#39;count&#39;&gt; &lt;script src=&#34;/feeds/posts/default?alt=json-in-script&amp;callback=mbhTotalCount&#34;&gt;&lt;/script&gt; &lt;/span&gt;</code></pre> </div> </div> <p></p> <p>我们如何将其更改为替代形式?</p> </question> <answer tick="false" vote="0"> <p>只需将变量分配给跨度的 <pre><code>innerText</code></pre>。</p> <p></p><div data-babel="false" data-lang="js" data-hide="false" data-console="true"> <div> <pre><code>&lt;script&gt; function mbhTotalCount(json) { var totalCount = parseInt(json.feed.openSearch$totalResults.$t, 10); document.getElementById(&#34;count&#34;).innerText = totalCount; } &lt;/script&gt; &lt;span class=&#39;count&#39;&gt; &lt;script src=&#34;/feeds/posts/default?alt=json-in-script&amp;callback=mbhTotalCount&#34;&gt;&lt;/script&gt; &lt;/span&gt;</code></pre> </div> </div> <p></p> </answer> </body></html>

回答 0 投票 0

在 document.write 中对于带有属性的标签使用正确的语法是什么?

我在JavaScript的document.write方法中使用以下代码: document.write("<font color="blue">["+time()...</desc> <question vote="1"> <p>我在 JavaScript 的 <em>document.write</em> 方法中使用以下代码:</p> <pre><code> &lt;script type=&#34;text/javascript&#34;&gt; document.write(&#34;&lt;font color=&#34;blue&#34;&gt;[&#34;+time()+&#34;]&lt;/font&gt;&#34;); &lt;/script&gt; </code></pre> <p>但这似乎不起作用。我该如何解决它?</p> </question> <answer tick="true" vote="4"> <p>这个问题与 HTML 没有直接关系,只是与字符串文字在 JavaScript(以及几乎所有其他编程语言)中的工作方式有关。如果您想在由 <pre><code>&#34;</code></pre> 字符分隔的 JavaScript 字符串中使用 <pre><code>&#34;</code></pre> 字符,则必须对其进行转义:<pre><code>\&#34;</code></pre>。或者使用 <pre><code>&#39;</code></pre> 来分隔字符串或属性值。</p> <pre><code>document.write(&#34;&lt;font color=\&#34;blue\&#34;&gt;[&#34;+time()+&#34;]&lt;/font&gt;&#34;); document.write(&#39;&lt;font color=&#34;blue&#34;&gt;[&#39;+time()+&#34;]&lt;/font&gt;&#34;); document.write(&#34;&lt;font color=&#39;blue&#39;&gt;[&#34;+time()+&#34;]&lt;/font&gt;&#34;); </code></pre> <hr/> <p>也就是说,使用 <pre><code>document.write</code></pre> 生成 HTML 通常不是一个好主意(它只在加载时工作,通常最好用更可靠的服务器端代码或更可重用的 DOM 操作替换))并且 <pre><code>font</code></pre> 元素已被弃用.</p> </answer> <answer tick="false" vote="2"> <pre><code>&lt;script type=&#34;text/javascript&#34;&gt; document.write(&#34;&lt;font color=\&#34;blue\&#34;&gt;[&#34; + time() + &#34;]&lt;/font&gt;&#34;); &lt;/script&gt; </code></pre> <p>或</p> <pre><code>&lt;script type=&#34;text/javascript&#34;&gt; document.write(&#39;&lt;font color=&#34;blue&#34;&gt;[&#39; + time() + &#39;]&lt;/font&gt;&#39;); &lt;/script&gt; </code></pre> <p>请注意,<pre><code>time</code></pre> 在 JavaScript 中并不作为函数存在——我假设您在上面创建了一个未包含在代码片段中的函数。另外,除非您只是在尝试,否则您不应该真正将 <pre><code>document.write</code></pre> 用于任何事情。您可以在 Firefox 的 Firebug 或 Safari / Chrome Web Inspector 中使用 <pre><code>console.log</code></pre> 来测试所有这些,比全页面刷新进行实验要快得多。还注意到我如何在 <pre><code>+</code></pre> 标志两侧添加空格:更容易阅读。</p> </answer> <answer tick="false" vote="0"> <p>您可以使用 CSS 代替 HTML,而且非常简单:</p> <pre><code>window.document.write(&#34;&lt;span style=\&#34;color:blue;font-size: 5em;\&#34;&gt;hello &#34; + variable + &#34;&lt;\/span&gt;&#34;); </code></pre> </answer> <answer tick="false" vote="-1"> <pre><code>var color = &#39;blue&#39;; document.write(&#34;&lt;font color=&#34;+color+&#34;&gt;[&#34;+time()+&#34;]&lt;/font&gt;&#34;); </code></pre> <p>或者</p> <pre><code> document.write(&#34;&lt;font color=&#39;blue&#39;&gt;[&#34;+time()+&#34;]&lt;/font&gt;&#34;); </code></pre> </answer> </body></html>

回答 0 投票 0

在 MutationObserver 中使用 document.write 会导致浏览器卡住并且 document.readyState 总是“正在加载”

免责声明:我知道为什么我应该避免使用 document.write 和 document.write 清除页面。 假设我需要在加载 DOM 时将同步(阻塞)脚本注入到 DOM 中(文档...

回答 0 投票 0

为什么document.write无法在带有HTML标签的变量上工作?

document.write上的变量工作正常。在带有HTML标记的字符串上进行document.write也可以正常工作。但是,当尝试在带有HTML标记的JS变量上使用document.write时,该程序将无法运行....

回答 1 投票 0

Javascript文本引擎一次键入所有字母,而不是按命令输入

我正在用javascript开发文本引擎,用户在其中输入一系列字母,这些字母会在屏幕上打印出来。我目前正在为每个单独的键使用if语句。为什么...

回答 1 投票 0

我如何读出Javascript中元素的位置?

我正在尝试用JavaScript编写一个小任务,使人们可以在屏幕上拖放两个对象。这里的网站:http://www.unisg.bplaced.net/JS/dragdrop.html我的目标是现在读出...

回答 2 投票 0

简单-从While循环的输出末尾删除逗号

var i = 1; while(i <100){i * = 2; document.write(i +“,”); }这样写:2、4、8、16、32、64、128,写128之后取逗号的最简单方法是什么?

回答 2 投票 0

错误TS2584(TS)的含义是什么…尝试更改'lib'编译器选项以包括'dom'?

我使用VS并编写Typescript,我只想键入命令“ document.write”而不会出现错误代码。我现在所拥有的是此命令“文档”的错误代码。我只是不想...

回答 2 投票 0

为什么在for循环中使用document.getElementById('')。innerHTML和document.write()的结果不同?

要清楚一下document.write(),我知道这已经过时了,不想重新加载并关闭网站。所以我想出了document.getElementById()。innerHTML作为替代方案。但是,...

回答 1 投票 1

为什么在for循环中使用document.getElementById('')。innerHTML和document.write()的结果不同?

要清楚一下document.write(),我知道这已经过时了,不想重新加载并关闭网站。所以我想出了document.getElementById()。innerHTML作为替代方案。但是,...

回答 1 投票 1

jsFiddle为什么不喜欢这个循环?

小提琴与在服务器上运行时实际起作用的javascript语句之间的差异似乎存在一些严重的问题。你们可以帮我解决这个问题吗?我有...

回答 4 投票 0

将其转换为document.write替代解决方案?

以下脚本本身可以很好地在页面上运行。但是,嵌入它时,document.write会接管,从而使页面的其余部分被遗忘。我想要一个替代解决方案...

回答 1 投票 1

在document.write中是否可以编写和执行i / else语句?

我正在尝试使文本随机化,这将根据给定的单词及其放置位置来创建新句子。我已经创建了名词,动词,数量等的数组。问题是,我需要它...

回答 2 投票 0

如何在网页中多次加载包含document.write的Javascript?

我在head标签中显示了一个javascript代码:document.write('.. .. ');代码...

回答 1 投票 1

JavaScript文件加载回退。替代document.write()

您可能熟悉旧的良好的Jquery负载回退:window.jQuery || document.write('<script src =“ https://example.com/jquery.js”>')但是我...

回答 2 投票 0

为什么使用document.write不会显示在浏览器上的文字

我有这样一个代码是一个js文件,另一个是HTML文件,问题是,当我运行的代码浏览器的犯规显示功能的文本文件撰写(...),有人能帮助我吗?功能...

回答 3 投票 1

终极版不注册document.open后的行动()

我有三个传奇侦听相同的动作,因此发生或多或少的同时。其中一人用重写页面:document.open();文件撰写(内容);在此之后发生的,...

回答 1 投票 1

Selenium使用document.write查找元素

我试图在selenium和python的网页上运行一些自动脚本,但我遇到的问题是使用document.write()加载网页。我必须找到一些元素,但他们......

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.