使用Javascript打开一个新页面并在那里填充表单值 [英] Using Javascript to Open a New Page and Populate Form Values There

查看:128
本文介绍了使用Javascript打开一个新页面并在那里填充表单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在书签中使用JavaScript来填充网站上的表单元素:

I am using JavaScript in a bookmarklet to populate form elements on a website:

javascript:var f = document.forms[0];
f.getElementsByTagName('input')[0].value = 'myname';
f.getElementsByTagName('input')[1].value = 'mypassword';
f.getElementsByTagName('input')[2].click

这有效。然而,我想创建的是一个bookmarklet,以便它打开目标页面,并在那里填充值;然而,似乎加载了页面,其他JavaScript代码不会被执行。因此,以下内容不起作用。

This works. However what I would like to create is a bookmarklet so that it opens the target page, and populates the values there; however it seems that onces the page is loaded, other JavaScript codes are not executed. So, the following doesn't work.

javascript:window.location("mywebsite");var f = document.forms[0];
f.getElementsByTagName('input')[0].value = 'myname';
f.getElementsByTagName('input')[1].value = 'mypassword';
f.getElementsByTagName('input')[2].click;

我还尝试使用setTimeout来延迟执行我的代码,但这不起作用。

I have also experimented with setTimeout to delay the execution of my code, but that didn't work.

javascript:var f = document.forms[0];setTimeout("f.getElementsByTagName('input')[0].value = 'myname';f.getElementsByTagName('input')[1].value = 'mypassword';f.getElementsByTagName('input')[2].click;",1000);

一旦我知道目标页面已满载,我该如何加载我的脚本?

How can I load my script once I know the target page is fully loaded?

推荐答案

Greasemonkey 内置于Chrome中,听起来就像是在尝试重新发明轮子。安装一个JS文件,它将在页面加载时运行。

Greasemonkey is built into Chrome, sounds like you are trying to reinvent the wheel. Install a JS file and it will run when the page loads.

这篇关于使用Javascript打开一个新页面并在那里填充表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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