最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I open a webpage and run some javascript functions? - Stack Overflow

programmeradmin1浏览0评论

Hi I would like to open a page and then run some javascript functions. My problem is that once I open the window it stops running the code:

javascript:
location=("/");
showForm();
document.getElementById("txtEmail").value="[email protected]";
submit();

Hi I would like to open a page and then run some javascript functions. My problem is that once I open the window it stops running the code:

javascript:
location=("http://www.myTestPage./");
showForm();
document.getElementById("txtEmail").value="[email protected]";
submit();
Share Improve this question edited May 28, 2012 at 5:31 Nakilon 35.1k16 gold badges111 silver badges148 bronze badges asked Oct 24, 2009 at 8:04 Deelo55Deelo55 511 silver badge4 bronze badges 1
  • Do you want to run javascript in the new window or the current one? – RichardOD Commented Oct 24, 2009 at 8:12
Add a ment  | 

5 Answers 5

Reset to default 4

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.

It's because your javascript functions are declared in the window object. By calling location= you destroy the current window object and all the function in it. 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.

location=("http://www.myTestPage./") starts the navigation to the new page. Where do you intent for showForm() to be called from? 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./"); 
showForm(); 
document.getElementById("txtEmail").value="[email protected]"; 
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); otherwise, you'll have to do as some others have suggested and have the target page handle it itself.

发布评论

评论列表(0)

  1. 暂无评论