Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

How to close a single tab in a browser (FF, IE, Chrome)


  • Please log in to reply
11 replies to this topic
nigello
  • Members
  • 31 posts
  • Last active: Sep 02 2015 09:28 AM
  • Joined: 25 Mar 2010
Greetings all!

I have been looking round for a way to close just a single specific tab in a browser. When in FF or IE or Chrome you can do ctrl+W and it will close just the active tab, but when i put ^w in a script which has found the tab it closes the entire browser instead (in any of those browsers).

i actually want to do this as a workaround for the real problem. I'll be happy if i can get the workaround, but if i describe the real problem someone might be able to help there too.

I am writing a script for my colleagues to send a selected word or phrase to a search tool we've developed in house. Although when the tool is loaded the cursor is supposed to be in the search field, if it is already open at the moment the search is requested there is no guarantee the cursor will still be there. (That's why i thought i'd close the tab and reload the site).

is there a way of directing the mouse to click in the search box, given that there is no telling how big the window will be on any given user's screen so there are no absolute coordinates? is there some point of reference in a web page that i can use to give relative coordinates? or is there a way of passing the "address" of the search box to an ahk script? is the site's source code of any use here (meaning does it contain anything that might help me)?

any help much appreciated!
regards
Nigel

  • Guests
  • Last active:
  • Joined: --
Can't you pass on the query via the url? Just like many people do with google <!-- m -->http://www.google.co...ch?hl=en&q=<!-- m -->%clipboard%

aRt)Y
  • Members
  • 447 posts
  • Last active: Oct 03 2013 08:40 PM
  • Joined: 17 Nov 2011

When in FF or IE or Chrome you can do ctrl+W and it will close just the active tab, but when i put ^w in a script which has found the tab it closes the entire browser instead (in any of those browsers).

I have added this a long time ago in my script and never closed the whole browser, except you only have 1 tab x.x
Maybe you are doing something wrong?

Lusein
  • Members
  • 5 posts
  • Last active: Mar 05 2012 02:07 PM
  • Joined: 08 Jul 2009
ctrl + w will close the active tab. On a side note if only one tab is open then it will close the browser.

Here are examples for navigating around browsers.

;Search

;IE  
ControlSend, Edit2, <text>
sentEvent, {enter}

;chrome
ControlSend, Chrome_AutocompleteEditView1, <text>
sentEvent, {enter}

;FireFox
sendEvent, ^k
send, <text>
sentEvent, {enter}

;URL Bar
;IE  
ControlSend, Edit1, <text>
sentEvent, {enter}

;chrome
ControlSend, Chrome_AutocompleteEditView1, <text>
sentEvent, {enter}

;FireFox
sendEvent, !d
send, {BS}
send, <text>
sentEvent, {enter}


nigello
  • Members
  • 31 posts
  • Last active: Sep 02 2015 09:28 AM
  • Joined: 25 Mar 2010

Can't you pass on the query via the url? Just like many people do with google <!-- m -->http://www.google.co...ch?hl=en&q=<!-- m -->%clipboard%


Thanks, but the search box doesn't have an address like this. it's a field set up in the page source and i don't know how i would specify it to direct the mouse to click there or send the text directly there.

nigello
  • Members
  • 31 posts
  • Last active: Sep 02 2015 09:28 AM
  • Joined: 25 Mar 2010

When in FF or IE or Chrome you can do ctrl+W and it will close just the active tab, but when i put ^w in a script which has found the tab it closes the entire browser instead (in any of those browsers).

I have added this a long time ago in my script and never closed the whole browser, except you only have 1 tab x.x
Maybe you are doing something wrong?


Thanks for replying!
This is what I do.
(btw I have adapted some code for looping thru tabs from jpjazzy @ <!-- m -->http://www.autohotke... ... ab browser<!-- m -->.
It works very well, so thanks very much!)
Edit 20 December: actually it doesn't seem to work as well as it did!

Browser_Open:  ;i.e. FF, IE or Chrome is found in use

;now I check whether OurTermBase is already open in a tab. 

WinActivate, %MyBrowser%
msgbox MyBrowser = %MyBrowser% ;(just a check in development)

SetTitleMatchMode, 2

;SetBatchLines, -1 ;not quite sure what this is for 

NumberOfTabsToCheck := 10 ; It will check this many tabs before giving up

{
TrayTip, Status:, Checking for OurTermBase...

   Loop, %NumberOfTabsToCheck%
   {
      Send, ^%A_Index% ; Switches browser tab using Ctrl+[no.] starting at 1 going to the Max
	  sleep, 1000
      
	  WinGetText, MyText , %MyBrowser% ; Get the text info for the tab
      
      ;TrayTip, Status:, Tab %A_Index% = %MyText% ;just a progress check during development)
	  
	  IfInString, MyText, OurTermBase ; If the tab title is OurTermBase, then stop
      {
         TrayTip, Status:, OurTermBase found!
         
;This is where the problem is:
		
	WinActivate, OurTermBase ; Trying to ensure that ctrl+w will only apply to the relevant tab, but it still doesn't work

	 send, ^W 		 ; trying to close tab ready to reload, so as to ensure cursor is in Search box and Search box is empty, ready for pasting of text, but it currently closes the entire browser even when multiple tabs are open

		 GoSub OurTermBase_search
      }
     
      If (A_Index = %NumberOfTabsToCheck%) ; If the last loop reaches this point no more can be done
      {
       TrayTip, Status:, Too many tabs open in your browser
      }
	  
   }
   
}
[/code]

Any ideas very welcome! I have a bit of a deadline on this!! (next Wednesday!!)

thanks
Nigel

nigello
  • Members
  • 31 posts
  • Last active: Sep 02 2015 09:28 AM
  • Joined: 25 Mar 2010

ctrl + w will close the active tab. On a side note if only one tab is open then it will close the browser.

Here are examples for navigating around browsers.
[...]


Thanks for replying!
Please see my reply to aRt)Y for my code.
thanks for the examples, but (correct me if I'm wrong) i take it these are for regular searches in the browser's own search field? We're not using those.

Regards
Nigel

  • Guests
  • Last active:
  • Joined: --

Thanks, but the search box doesn't have an address like this. it's a field set up in the page source and i don't know how i would specify it to direct the mouse to click there or send the text directly there.

You don't understand I'm afraid.

If you run your application does the address in the location bar of your browser change? For example you search for "word' and it starts with:
<!-- m -->http://intranet/sear....php?query=<!-- m -->word&count=10 for example you can then simply RUN the query. Look at google before/after a search in the browser location bar (the URL)

nigello
  • Members
  • 31 posts
  • Last active: Sep 02 2015 09:28 AM
  • Joined: 25 Mar 2010

You don't understand I'm afraid.

If you run your application does the address in the location bar of your browser change? For example you search for "word' and it starts with:
<!-- m -->http://intranet/sear....php?query=<!-- m -->word&count=10 for example you can then simply RUN the query. Look at google before/after a search in the browser location bar (the URL)


Thanks for the suggestion. it would be great if we could do that, but in our case neither the search term nor the result affects the location or address. it's a database set up with MySQL, if that helps to explain the issue better.

forgive me if i don't set it out terribly well. i know quite a lot but just not quite enough so there are technical points i don't understand.

regards
Nigel

  • Guests
  • Last active:
  • Joined: --

Thanks for the suggestion. it would be great if we could do that, but in our case neither the search term nor the result affects

... if it is developed in-house you could ask the developers to change from get to post method for the form (your devs should know) or the other way around (can never remember) to support it.

nigello
  • Members
  • 31 posts
  • Last active: Sep 02 2015 09:28 AM
  • Joined: 25 Mar 2010

Thanks for the suggestion. it would be great if we could do that, but in our case neither the search term nor the result affects

... if it is developed in-house you could ask the developers to change from get to post method for the form (your devs should know) or the other way around (can never remember) to support it.


yes i could ask her. it's an idea to look at. but i can't understand why ^w shouldn't work. is it case-sensitive? several people have said it works for them. maybe it's my code.

regards
N.

Lusein
  • Members
  • 5 posts
  • Last active: Mar 05 2012 02:07 PM
  • Joined: 08 Jul 2009

thanks for the examples, but (correct me if I'm wrong) i take it these are for regular searches in the browser's own search field? We're not using those.


You are correct those are the regular searches. I missed the part about the search being on the page.