页面加载后Javascript运行功能 [英] Javascript Run Function After Page Load

查看:143
本文介绍了页面加载后Javascript运行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Javascript中创建一个小型自动化脚本,我想使用Opera的用户脚本功能运行一个站点,以定义要运行的外部脚本。我之前使用过这个功能来运行我用外部网站编写的脚本,效果很好。

I'm trying to create a small automation script in Javascript that I want to run with a site using Opera's User Script feature to define external scripts to run. I have used this feature before to run scripts I wrote with external sites, to nice effect.

我需要等到页面加载才能运行脚本,但我似乎无法让它工作。代码目前是:

I need to wait till the page loads for the script to run, but I can't seem to get this to work. The code currently is:

if (addEventListener in document) { // use W3C standard method
    document.addEventListener('load', meerfirst(), false);
} else { // fall back to traditional method
    document.onload = meerfirst();
}

function meerfirst(){
    nameForm = document.forms['aspnetForm'];
    nameForm.elements('ctl00$CPH1$NewQuoteView$TitlesView$DropDownListTitles').value = 'MR:TRUE:MR';
    nameForm.elements('ctl00$CPH1$NewQuoteView$TextBoxFirstName').value = 'James';
 }

这是我自己的函数,添加了通过另一个问题找到的if语句这里。我也试过window.onload,但它仍然无法正常工作。

This is my own function with the addition of the if statement found via another question here. I have also tried window.onload, but it still didn't work.

奇怪的是Opera根本就没有真正执行脚本,好像我在if语句上设置了一个断点,它从来没有实际打破它。该网站是否内置了反用户脚本功能?或者有可能我做错了什么来阻止这个执行?

Strangely Opera doesn't really seem to execute the script at all, as if I set a breakpoint on the if statement it never actually breaks on it. Could the site have a anti-userscript feature built-in? Or is there possible something I'm doing wrong to stop this executing?

推荐答案

此代码有几个问题:

if (addEventListener in document) { // use W3C standard method
    document.addEventListener('load', meerfirst(), false);
} else { // fall back to traditional method
    document.onload = meerfirst();
}




  1. 你需要在周围加上报价if语句中的addEventListener。您在文档中寻找属性名称,因此名称必须是字符串 if(文档中的'addEventListener')

  1. You need to put quotes around addEventListener in the if statement. You're looking for a property name in document, so the name needs to be a string: if( 'addEventListener' in document )

您想在addEventListener语句中按名称引用该函数,不要立即打电话。你应该删除括号 - 只使用 meerfirst 而不是 meerfirst()

You want to refer to the function by name in the addEventListener statement, not call it immediately. You should remove the parentheses - use just meerfirst instead of meerfirst().

分配给document.onload无效。请参阅我对 window.onload vs document.onload 的回答,并使用window.onload。

Assigning to document.onload has no effect. See my answer to window.onload vs document.onload and use window.onload instead.

将作业更改为窗口 .onload后,您还需要删除括号。同样,你只是按名称引用该函数,并且不希望此时实际调用它。

After changing the assignment to window.onload you also need to remove the parentheses. Again, you're just referring to the function by name and don't want it to actually be called at this point.

最后,我建议你听一下 DOMContentLoaded 而不是加载 - 除非您要运行一些需要等待所有图像加载的代码。

Finally, I recommend listening for DOMContentLoaded rather than load - unless you are going to run some code that needs to wait until all images are loaded.

这篇关于页面加载后Javascript运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆