简体   繁体   English

如何在 html 元素中加载外部 javascript function?

[英]How do I load an external javascript function in an html element?

I have a sample.js file which contains我有一个sample.js文件,其中包含

document.write('<script>........</script><a>...</a><style>....</style>');

And I have this script <script src="https://example.com/sample.js"></script> in external website.我在外部网站上有这个脚本<script src="https://example.com/sample.js"></script> Where-ever I use this script, sample.js is loaded immediately.无论我在哪里使用这个脚本, sample.js都会立即加载。 And all other elements are not removed even though I use document.write即使我使用document.write也不会删除所有其他元素

I want to load the above script in multiple Html <div> or other elements, where we define something like id="example" or class="example" or data-sample-js我想在多个 Html <div>或其他元素中加载上述脚本,我们在其中定义类似id="example"class="example"data-sample-js

So how do I modify the sample.js file to achieve this.那么如何修改sample.js文件来实现这一点。

So far, I have tried in the sample.js :到目前为止,我已经在sample.js中尝试过:

window.addEventListener('DOMContentLoaded', (event) => {
document.querySelectorAll("[data-sample-js]").forEach(elem => {
 elem.innerHTML = document.write('<script>...</script><a>...</a><style>...</style>');
});});

So where-ever we place the <script src="https://example.com/sample.js"></script> along with <div data-sample-js></div> the javascript is loaded but it will remove all other html elements in the page.因此,无论我们将<script src="https://example.com/sample.js"></script><div data-sample-js></div>放在哪里,javascript 都会被加载,但它会删除页面中的所有其他 html 元素。

EDIT:编辑:

There is a full html document is placed in the document.write(). 
It can have multiple scripts, styles, metas and all other possible codes.
that will be present in a static html webpage, including <script src="...">

I'm trying to work out exactly what you mean.我正在努力弄清楚你的意思。 From what I can infer, you would like the code in sample.js to be applied to all elements that have a specific characteristic rather than purely executing the code.根据我的推断,您希望将sample.js中的代码应用于具有特定特征的所有元素,而不是纯粹执行代码。

For this you'll want to use the likes of element selectors eg为此,您需要使用元素选择器之类的东西,例如

document.getElementByClassName(string);
document.getElementByTagName(string);
document.querySelectorAll(string);

More can be found here.更多可以在这里找到。

https://plainjs.com/javascript/selecting/ https://plainjs.com/javascript/selecting/

An example of using these would be:使用这些的一个例子是:

 document.querySelectorAll("[data-sample-js]").forEach(elem => { elem.innerText += "This is a sample text"; });
 <div data-sample-js>Test</div>

Which will concatenate This is a sample text onto the content of any element with data-sample-js as an attribute.它将连接This is a sample text到任何元素的内容上,其中data-sample-js作为属性。

Note that this code must be imported at the end to ensure that all elements are added to the DOM and loaded before the script takes place.请注意,必须在最后导入此代码,以确保在脚本发生之前将所有元素添加到 DOM 并加载。

EDIT 1:编辑1:

As mentioned in these comments, do not use document.write source如这些评论中所述,请勿使用document.write

Instead, I recommend using a different method for loading this content.相反,我建议使用不同的方法来加载此内容。 Stylesheets can be added by simply adding the <style> to the head of the page and for scripts use a dynamic loader.可以通过简单地将<style>添加到页面头部来添加样式表,并且对于脚本使用动态加载器。 More information on <script> imports can be found here .更多关于<script>导入的信息可以在这里找到。

EDIT 2:编辑2:

It appears you are trying to write frontend JavaScript code to dynamically generate a page.看来您正在尝试编写前端 JavaScript 代码来动态生成页面。 You should not do this and instead take care of this in the backend application that serves the page.您不应这样做,而应在为页面提供服务的后端应用程序中处理此问题。 This is fairly simple in most backend languages / frameworks (PHP can simply use require_once ).这在大多数后端语言/框架中都相当简单(PHP 可以简单地使用require_once )。

Maybe this is what you trying to achieve.也许就是您想要实现的目标。

Long story short:长话短说:

var scriptTag = document.getElementsByTagName('script');
scriptTag = scriptTag[scriptTag.length - 1];
var parentTag = scriptTag.parentNode;
parentTag.innerHTML = 'This is a sample text to replace its content';
parentTag.classList.add('new_class');
parentTag.id = 'new_id';

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

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