简体   繁体   English

如何打开网页并运行一些javascript函数?

[英]How do I open a webpage and run some javascript functions?

Hi I would like to open a page and then run some javascript functions. 嗨,我想打开一个页面,然后运行一些JavaScript函数。 My problem is that once I open the window it stops running the code: 我的问题是,一旦我打开窗口,它就会停止运行代码:

javascript:
location=("http://www.myTestPage.com/");
showForm();
document.getElementById("txtEmail").value="test@hotmail.com";
submit();

You can't. 你不能。 The problem is that each page is loaded into its own logical window (even if that window occupies the same client area in the browser as the previous page). 问题是每个页面都被加载到它自己的逻辑窗口中(即使该窗口占用浏览器中与前一页面相同的客户区域)。 Each window runs script in its own context. 每个窗口都在自己的上下文中运行脚本。 Usually when windows are replaced any running script is terminated and even if it weren't I suspect you want the code following the location assignment to operate on the new content. 通常在更换窗口时,任何正在运行的脚本都会终止,即使不是,我怀疑您希望位置分配后的代码对新内容进行操作。

You would need the target page to run your code for you. 您需要目标页面来为您运行代码。 If the page is generated dyanmically by something like PHP or ASP then you could use the query string to specify a file that the page should point the SRC of a script block it puts at the bottom of the body content. 如果页面是由PHP或ASP之类的动态生成的,那么您可以使用查询字符串来指定一个文件,该页面应该指向它放在主体内容底部的脚本块的SRC。

It's because your javascript functions are declared in the window object. 这是因为你的javascript函数是在window对象中声明的。 By calling location= you destroy the current window object and all the function in it. 通过调用location =你可以销毁当前窗口对象及其中的所有函数。 After all you cant declare function in one window to run in the same same window but with another location. 毕竟你不能在一个窗口中声明函数,以在同一个窗口中运行,但在另一个位置运行。 All you can do is toopen a new window. 你所能做的就是打开一个新窗口。

It is because the page has transferred to a new location. 这是因为页面已转移到新位置。 Execute your javascript first before you move to another location. 在移动到其他位置之前先执行您的javascript。

location=("http://www.myTestPage.com/") starts the navigation to the new page. location=("http://www.myTestPage.com/")开始导航到新页面。 Where do you intent for showForm() to be called from? 你打算从哪里调用showForm() If it's the current page, I don't get why you want to do that? 如果它是当前页面,我不明白为什么你想这样做?

This will following though I doubt you want to open a new window, yea? 虽然我怀疑你想打开一个新窗口,但是会这样吗?

window.open("http://www.myTestPage.com/"); 
showForm(); 
document.getElementById("txtEmail").value="test@hotmail.com"; 
submit();

To Add: 加上:

I think you wanted to submit the form to for server-side process and also navigate to the new location at the same time. 我想你想将表单提交给服务器端进程,同时也要导航到新位置。 Few ways to do it: 几种方法:

  1. Submit the form, and let the response redirect to the desired location 提交表单,让响应重定向到所需位置

  2. Submit the form asyncronously, after that navigate to new page 异步提交表单,然后导航到新页面

This is only possible in JavaScript if you open the second page in a new window and that page is hosted on the same domain (since JavaScript has a same-domain security policy); 如果您在新窗口中打开第二页并且该页面托管在同一个域中(因为JavaScript具有相同的域安全策略),则只能在JavaScript中使用; otherwise, you'll have to do as some others have suggested and have the target page handle it itself. 否则,你将不得不像其他人建议的那样做,并让目标页面自己处理它。

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

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