如何使用javascript自动关闭网页? [英] How to automatically close webpage using javascript?

查看:126
本文介绍了如何使用javascript自动关闭网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿大家,



我在不同的论坛上发现了许多类似我的问题,但我找不到真正有助于解决我的问题的答案它们。



基本上,我想要的是通过命令提示符打开一个URL,它将打开浏览器,处理一个网页,然后我希望它自动关闭。



我为什么需要这个?

我有一个在IIS上运行的应用程序。我需要在我的应用程序中每天运行一些例程。我可以通过运行类似于下面显示的URL来简单地执行这些例程:



http://myapplication.com/DoStuff.aspx?



问题是这完全是手动的。

我想知道我是否可以创建一个批处理文件,调用我的URL启动http://myapplication.com/DoStuff.aspx?,然后我可以在Windows上创建一个任务来运行该批处理每天归档。除了浏览器不会自动关闭之外,这对我有用。

我的意思是,我可以尝试一下,但在一周结束时,我会打开至少5个窗口。



我尝试了什么:



我试图通过使用javascript来解决它,但我总是收到这条消息:

Hey everybody,

I have found many questions like mine in different forums, but I couldn't find an answer that actually helps to solve my problem in any of them.

Basically, what I want is to open an URL through command prompt, it will open the browser, processes a webpage, and then I would like it to automatically close.

Why do I need this?
I have an application that runs on IIS. There are some routines I need to run everyday in my application. I can simply kick of these routines by running an URL similar to the showed below:

http://myapplication.com/DoStuff.aspx?

The Problem is that this is totally manual.
I was wondering if I could create a batch file calling my URL "start http://myapplication.com/DoStuff.aspx?", and then I could create a task on Windows to run that batch file everyday. That works for me except that the browser will not close automatically.
What I mean is, I could try it, but at the end of a week, I would have at least 5 windows opened.

What I have tried:

I have tried to solve it by using javascript, but I always end up getting this message:

scripts may close only the windows that were opened by it





没关系我使用window.close()创建的javascript函数,窗口不会关闭。



Chrome和Firefox返回该消息。

IE让我尝试关闭窗口,但是如果我真的要关闭它,它会在popUp中询问。



It does not matter the javascript function I create using "window.close()", the windows won't close.

Chrome and Firefox returns that message.
IE let's me try to close the window, but it asks in a popUp if I really want to close it.

推荐答案

你试图回答错误题。 :)



不要试图找到一种方法来使用Javascript来关闭浏览器窗口。即使你发现了一个让你这样做的黑客攻击,它也会成为浏览器中的一个错误,很可能会修补你的脚本。



相反,使用不同的工具来发出请求。例如,Powershell可以在不在浏览器中打开URL的情况下请求URL:

You're trying to answer the wrong question. :)

Don't try to find a way to use Javascript to close the browser window. Even if you find a hack to let you do that, it will be a bug in the browser, and will most likely be patched, breaking your script.

Instead, use a different tool to make the request. For example, Powershell can request a URL without opening it in a browser:


url =http://myapplication.com /DoStuff.aspx?
url = "http://myapplication.com/DoStuff.aspx?"


webclient = new-object System.Net.WebClient
webclient = new-object System.Net.WebClient


这篇关于如何使用javascript自动关闭网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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