开发者

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. My problem is that once I open t开发者_StackOverflow社区he 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.


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.com/") 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.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); otherwise, you'll have to do as some others have suggested and have the target page handle it itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜