Javascript 如何自动执行javascript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2644615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:17:14  来源:igfitidea点击:

how do I automatically execute javascript?

javascript

提问by Sarfraz

how do I automatically execute javascript?

如何自动执行javascript?

I know of <body onLoad="">, but I just thought maybe there is another way to do it?

我知道<body onLoad="">,但我只是想也许还有另一种方法可以做到?

html:

html:

<html><head></head><body><div id="test"></div></body></html>

javascript:

javascript:

<script>(function(){var text = document.getElementById('test').innerHTML;var newtext = text.replace('', '');return newtext;})();</script>

I wanna get the text within "test", replace certain parts, and then output it to the browser.

我想在“测试”中获取文本,替换某些部分,然后将其输出到浏览器。

Any ideas on how to do it? I'd appreciate any help. Thanks.

关于如何做到这一点的任何想法?我很感激任何帮助。谢谢。

回答by Sarfraz

If you don't want to use <body onload>which is good choice in terms of obtrusivejavascript, you can separate that and put you code like this:

如果您不想使用<body onload>哪个是突兀的javascript方面的好选择,您可以将其分开并放置如下代码:

window.onload = function(){
  // your code here
};

Alternative:

选择:

Place your javascript code at the bottom of the page.

将您的 javascript 代码放在页面底部。

回答by Sky Sanders

Place the script at the bottom of the page, outside the closing body tag..

将脚本放在页面底部,结束正文标记之外。

回答by user2993258

It's REALLY easy! If you have a script in your "head" block, with no function id, it will run automatically as soon as the web page loads. For example:

这真的很容易!如果您的“head”块中有一个没有函数 id 的脚本,它会在网页加载后立即自动运行。例如:

<head><meta charset="UTF-8"><title>Redirection to www.mywebsite.org</title>

<head><meta charset="UTF-8"><title>Redirection to www.mywebsite.org</title>

<!-- This script initiates an automatic web page redirection, as the page is loaded -->
<script type="text/javascript">
window.location = "http://www.mywebsite.com/"
</script>

</head>

</head>

回答by rochal

If you don't want to use jQuery, use native window.onload method:

如果您不想使用 jQuery,请使用原生 window.onload 方法:

<script type="text/javascript">
    function ReplaceText() {
        document.getElementById('test').innerHTML = document.getElementById('test').innerHTML.replace(/abc/g, "def");
    }
    window.onload = ReplaceText;
</script>

Used on the code:

在代码上使用:

<div id="test">abc abc</div>

Will give this output:

将给出这个输出:

def def

回答by Dan Diplo

A quick way, if you just want to debug, would be move what you want to execute outside of a function.

如果您只想调试,一种快速的方法是将要执行的内容移到函数之外。

<script type="text/javascript">
var text = document.getElementById('test').innerHTML;
var newtext = text.replace('', '');
alert(newtext);
</script>

NB. I'm not sure what you hope to achieve with text.replace('', '')?

注意。我不确定你希望实现text.replace('', '')什么?