This Forum has been archived

Forums: Admin Central Index Technical Help Open search results in new tab. JS or CSS?
Wikia's forums are a place for the community to help other members.
To contact staff directly or to report bugs, please use Special:Contact.


Is there a way to make search open in a new tab? I hate when it covers up my existing page, and I don't want to have to open up a throwaway page just to use search. This is such a poor design feature of the MediaWiki software.

Anyway, any ideas on how to fix this just for myself with JS or CSS? And what about for a whole wiki? For me personally, I use the Firefox browser. ---Timeshifter (talk) 02:32, March 9, 2012 (UTC)

You could use JS to add a target attribute:
$(function () {
    $('#WikiaSearch').attr({ target: '_blank' });

See here for info about JS files.-- Pecoes 10:31, March 9, 2012 (UTC)
Thanks! Works great here:
That JS and CSS cheat sheet is very helpful. ---Timeshifter (talk) 12:23, March 9, 2012 (UTC)
I noticed one problem though. When I search from Special:Search it does not open the search results in a new tab. Can this also be done? ---Timeshifter (talk) 15:20, March 9, 2012 (UTC)
That's a different form. This should do it:
$(function () {
    $('#WikiaSearch, #search').attr({ target: '_blank' });

-- Pecoes 16:54, March 9, 2012 (UTC)

Thanks. I put it here again:
I substituted this JS code for the other code and it works on the Community Wiki, but only partially on my main wiki, the cannabis wiki. Results of Special:Search opens up in a new tab here, but not on the cannabis wiki: ---Timeshifter (talk) 07:53, March 10, 2012 (UTC)
I just tested it and it worked fine. Were you logged in when you tested this? User:Timeshifter/global.js will work only for you and only as you're logged in. -- Pecoes 08:38, March 10, 2012 (UTC)
Yes, I am logged in. Search results open in a new tab here:
but not here:
Maybe it has to do with the JS and CSS Cheatsheet info about the upcoming Special:Mypage/common.js changes? See:
That is working on all skins (Wikia and Monobook) on the Cannabis Wiki. Is anything there interfering with your JS I added here?:
Also, I am an admin on the cannabis wiki. Maybe that has something to do with it. I don't know. ---Timeshifter (talk) 09:19, March 10, 2012 (UTC)
The fact that you're an admin doesn't enter into it and the future functionality of Special:Mypage/common.js is irrelevant now. It should work. It does for me and I'm using the same code. Try to refresh and if that doesn't help purge. -- Pecoes 09:43, March 10, 2012 (UTC)
It is working now! Out of curiosity I tried it again without refreshing or purging first. I had already refreshed before. So maybe enough time elapsed since the last time I was here for the server to purge its cache.
I added it also to here just now, and did a hard refresh:
I logged out, and search results from both forms open in a new tab. Is this OK to do for a wiki? Does Wikia mind? ---Timeshifter (talk) 13:09, March 10, 2012 (UTC)
I can't imagine why they would mind. The rule of thumb seems to be: As long as you obstruct neither ads nor content you can do whatever you like. This little change does neither. -- Pecoes 13:20, March 10, 2012 (UTC)
Tagging {{FAQ}}. -- Fandyllic (talk · contr) 12 Mar 2012 12:57 PM Pacific

The changes made so far have been greatly useful to me so far. I do many more searches now with Wikia's search engine instead of Google site searches. I use the Firefox browser. Google does not provide a Google Toolbar for Firefox. So previously I was frequently using the Google site searches in the search menu in the Cannabis Wiki. But it was a 2-step process that required opening up a new search tab first. Now I can search immediately from Wikia's search.

There is another problem. When search finds something that matches the search term, then the page opens in the same tab. For example; when the search term comes up in the dropdown list of suggestions. Try searching for "Paris" to see what I mean. It happens when searching for many city names because many cities have pages, and because there are many redirects from the basic city names to the full city+state+nation article title. So if one clicks on an item in the dropdown list of search suggestions then it opens in the same tab.

Special:Search results on the Cannabis Wiki and on the Community Wiki no longer open in a new tab. Maybe all these problems are related. ---Timeshifter (talk) 08:55, March 15, 2012 (UTC)

I'm sorry but I can't reproduce your problem. I loaded the exact same code that you have in w:c:cannabis:MediaWiki:Common.js and the exact same code you have in w:User:Timeshifter/global.js and it all worked fine. Do you have code anywhere else? -- Pecoes 17:40, March 15, 2012 (UTC)
I am trying to figure it out. Created this as an aid:
Will take more time, probably tomorrow. ---Timeshifter (talk) 19:12, March 15, 2012 (UTC)

I am wondering how much of this legacy CSS in Common.css is really necessary:

I am thinking of deleting all of the legacy CSS since it was ignored by Wikia.css unless one made a special effort to import it. See:

I only want to keep the CSS I added since I know more about what it is for. ---Timeshifter (talk) 22:14, March 15, 2012 (UTC)

I looked over the CSS but I have no idea what most of is/was needed for. I'm not exactly an old hand, you know. My MediaWiki/Wikia knowledge is barely two months old...
How about the open-search-in-new-page script? Does that work now? I looked over all of your script files and I can't find anything that would conflict with it. The only suggestion I could make is to open your web browser's error console and keep an eye open for JavaScript errors. There's the remote possibility that something else doesn't work and prevents this snippet from working as well. That's all I can think of at any rate.
(My fancy new signature almost works now. Sorry for posting all that messy raw code in the other thread!)

11:32, March 17, 2012 (UTC)

I will need some time to remove all the old CSS code and see if the wiki works OK without it. In the meantime there is the same problem. Searches open in a new tab from all search forms except when one clicks on one of the suggestions from the dropdown menu. Maybe that is not considered a search, but is considered more in the way of just clicking a link. ---Timeshifter (talk) 15:40, March 17, 2012 (UTC)
I removed a bunch of old CSS here:
I looked at all the CSS and JS pages listed here for stuff that could be deleted:
Everything remaining is being used. I purged and refreshed yesterday.
I still have the same problem on Firefox when clicking search dropdown links that I mentioned in my last comment. Everything else is working fine, and search results open in a new tab. Only the search dropdown links open in the same tab.
I tested on Internet Explorer too. Same problem, plus an additional problem. Special:Search does not open in a new tab under any circumstances. I am logged in, too. ---Timeshifter (talk) 22:21, March 20, 2012 (UTC)
I can confirm that clicking on a link from that dropdown menu won't open the results in a new tab. That's because these suggestions are fetched via AJAX and there's no actual submit. The JavaScript routines bypass the HTML form.
Strangely enough the suggestions dropdown has stopped working altogether. It looks like Wikia staff is tinkering with it... --  pecoes  11:50, March 21, 2012 (UTC) 
The suggestions dropdown is working now for me. Nothing drops down of course if the search term is not found in the pages of the wiki that are searched. Not all pages are searched from the search form at the top right of pages.
On the Wikimedia Commons I pointed to this forum and suggested opening search results in a new tab. It has been implemented (via a JS import)! See:
commons:Commons:Requests for comment/improving search#Open search results in a new tab. ---Timeshifter (talk) 18:40, March 23, 2012 (UTC)

(unindent). Clicking on search suggestions (from the dropdown menu) is still not opening in a new tab in the cannabis wiki or community wiki.

It works on the Wikimedia Commons. See:

JS on the Commons that fixes this problem:

JS importing instructions and code:

I like that this JS modification is imported on the Commons. That way it is automatically updated for all users who are using it. This is important if the overall MediaWiki software updates and changes effect search in ways that require updates in this JS modification. ---Timeshifter (talk) 15:48, March 25, 2012 (UTC)

So the solution they gave you at doesn't work after all? I gotta say: I was a little surprised, when you told me it did. I took your word for it though. :)
I looked at the searchform at WikiMedia and it has nothing in common with the one at Wikia. That one uses a plain HTML select and submits the form when you've picked a suggestion. This one does not use a select and it does not submit the form. Apples and oranges.
Poring over oasis_shared_js I found the instruction that opens the link. It is encapsulated in WikiaSearchApp.initSuggest. You might get away with overriding WikiaSearchApp.initSuggest:
$(function () {
    $('#WikiaSearch, #search').attr({ target: '_blank' });
WikiaSearchApp.initSuggest = function () {
    $.loadJQueryAutocomplete(function () {
            serviceUrl: wgServer + wgScript + "?action=ajax&rs=getLinkSuggest&format=json",
            onSelect: function (a, b) {
                WikiaSearchApp.trackInternal("search_start_suggest", {
                    sterm: encodeURIComponent(a.replace(/ /g, "_")),
                    rver: 0
      \$1/, encodeURIComponent(a.replace(/ /g, "_"))));
            appendTo: "#WikiaSearch",
            deferRequestBy: 250,
            maxHeight: 1000,
            selectedClass: "selected",
            width: "270px",
            skipBadQueries: true
I tested it and it seems to work. --  pecoes  21:13, March 25, 2012 (UTC) 

(unindent). The other JS is working great now on Wikipedia and the Wikimedia Commons. They are 2 different wikis. Importing from one wiki to the other did not work. So I pasted the other JS directly into the Wikipedia JS page, and it works perfectly. All search results and search suggestions open in new tabs.

Out of curiosity I tried the Commons JS on Wikia. It did not work well. ---Timeshifter (talk) 01:53, March 26, 2012 (UTC)

Next I pasted your latest JS here: Community:User:Timeshifter/global.js
I removed the old JS from here: w:c:cannabis:User:Timeshifter/common.js
I did a hard refresh (Ctrl-F5) and used the purge button. Search results and suggestions from the search form found at the top right of all pages open in new tabs. Both on the Cannabis Wiki and the Community Wiki.
w:c:cannabis:Special:Search works perfectly on the Cannabis Wiki. Everything opens in new tabs, both search results and search suggestions.
Special:Search on the Community Wiki does not work right at all. It opens searches in the same tab. It opens search suggestions in the same tab.
I looked at all my JS pages on the Community Wiki:
Help:JavaScript and CSS Cheatsheet
All my JS pages on the Community Wiki are empty except for User:Timeshifter/global.js - it contains the JS for opening search results in new tabs. ---Timeshifter (talk) 02:16, March 26, 2012 (UTC)
I have no explanation for that, I'm afraid. I just tested the code here at CC but there were no surprises. It opened all search results from regular pages, from Special:Search and on all pages from the suggestions in new tabs. --  pecoes  04:26, March 26, 2012 (UTC) 
The weird thing is that now w:c:cannabis:Special:Search no longer opens in new tabs (both search results and suggestions). It opened in new tabs when I wrote my last message.
Where exactly did you post the JS for your use? ---Timeshifter (talk) 05:26, March 26, 2012 (UTC)
In my sandbox wiki and here at cc. Both work. --  pecoes  05:30, March 26, 2012 (UTC) 
So is the new page supposed to open in a new tab when you click a search results link? Unfortunately, w:c:pecoes:Special:Search seems to be busted, since I searched for "Sandbox" and got no results. -- Fandyllic (talk · contr) 27 Mar 2012 4:46 PM Pacific
I disabled it :) Try again now! --  pecoes  00:58, March 28, 2012 (UTC) 
Thanks. It worked from the drop-down, but I couldn't test it from search results becuz I'm still not getting any. However, it seems to work nicely. I may try to enable it on WoWWiki soon, but I'll put it on WoWRP first. WoWWiki has some quirks that are different from standard Wikia wikis. The Mini Editor was broken on WoWWiki, but no where else. -- Fandyllic (talk · contr) 27 Mar 2012 5:10 PM Pacific

(unindent). w:c:cannabis:Special:Search has both search forms. One on top of the other. The top one opens in new tabs. The advanced search form below opens in the same tab. It is easy to start entering search terms that will pull up some suggestions in the dropdown menu. Try "global" or "cannabis". That is a way to test clicking suggestions.

This was with your JS, Pecoes, in only one JS page:

I removed that JS from all other pages to avoid problems. This way I can test on a wiki that I control the JS for. That means removing the JS from here too:

You, Pecoes, also need to remove it from Community:Special:Mypage/global.js - in order to get an accurate view of what is happening on Cannabis Wiki search. At least according to this great page of yours:

Both search forms open in new tabs for me. They open in a new tab when I type in a search term and hit return and they open in a new tab if I type in a search term and pick a suggestion from the dropdown.
Which browser are you using? Did you make any settings for new windows and/or new tabs in your browser?
Also: What are your "Search" preferences here at Wikia? --  pecoes  04:41, March 28, 2012 (UTC) 
I am using the Firefox browser. I have it set to "open new windows in a new tab instead".
In my Wikia preferences "Otherwise search in these namespaces" is set to search in some selected namespaces.
Did you do your tests using w:c:cannabis:Special:Search?
I see that your search JS is not here: Community:User:Pecoes/global.js
That is good, because that means you would be using the same search JS that I am using on the Cannabis Wiki. ---Timeshifter (talk) 10:58, March 28, 2012 (UTC)
Yes, I've been using w:c:cannabis:Special:Search. Again: Both search boxes worked for both kinds of searches.
This is a wild guess but maybe it has something to do with your Firefox settings... Please take a look at about:config and tell me the values of and --  pecoes  11:52, March 28, 2012 (UTC) 
They were at default settings. I uninstalled Firefox and reinstalled Firefox. I did not save any addons or settings. I have not yet installed any addons.
Both search forms at w:c:cannabis:Special:Search now open in new tabs when I am not logged in. I did not think to test that before reinstalling Firefox.
But (and this is key) only the top search form opens in new tabs when I am logged in. So it may be one of my Wikia preferences. I will go back to default preference settings and start testing again.
Or it could be something in my JS pages. ---Timeshifter (talk) 21:31, March 28, 2012 (UTC)

Default Wikia preferences solves problem

OK, it works correctly now after going back to default Wikia preferences. I will change preferences to my preferred settings one by one on the Cannabis Wiki until I find the one that is causing the problem. --Timeshifter 21:50, March 28, 2012 (UTC)

Odd. Do let me know which one was the culprit, please! Maybe there's still a limitation in the code that I need to fix... --  pecoes  21:55, March 28, 2012 (UTC) 
Out of curiosity I just tried your simpler JS for a bit. It works perfectly on the bottom advanced search form. But not on Wikia's search form on top (on nearly all pages). It does not open search suggestions in new tabs. --Timeshifter 22:01, March 28, 2012 (UTC)
The "simpler" JS? What are you referring to? --  pecoes  22:11, March 28, 2012 (UTC) 

(unindent). Simpler JS:

$(function () {
    $('#WikiaSearch, #search').attr({ target: '_blank' });

I went back to the much more complex JS.

I found the problem Wikia preference in the "Under the hood" tab. In the section called "Otherwise search in these namespaces." The default setting is to only search portal pages. When I also checked the boxes for "Main" and "File" then w:c:cannabis:Special:Search stopped working correctly. The bottom advanced search form opens all searches and suggestions in the same tab. The top search form works correctly and opens all searches and suggestions in a new tab. --Timeshifter 22:19, March 28, 2012 (UTC)

Nice detective work! :) I'll get back to you when I have a work-around! --  pecoes  22:31, March 28, 2012 (UTC) 
The problem is advanced search. Even when using the default Wikia preference. Once one clicks on the advanced search all search results and suggestions open in the same tab.
--Timeshifter 22:33, March 28, 2012 (UTC)
@Pecoes: Advanced search appears to be totally broken on your sandbox wiki. Does it work for you? -- Fandyllic (talk · contr) 29 Mar 2012 3:16 PM Pacific
Yes, it does work. There's not much to search for, though... Try "Sandbox".
And, er, I'm sorry, but clicking on a suggestion and clicking on the search button opens the result in a new tab. That works for the regular Search as well as the Advanced Search. I've also tried adding "Main" and "FIle" to my list of namespaces to search in, but that did not stop my code from working either.
There's only one solution left methinks. You and I are not using the same Wikia code. I'm a much "younger" Wikia user than the both of you. Maybe I'm using some brand new code, while you guys use legacy code. Or maybe we use the same code, but different version of it, because not everything is rolled out side-wide right away....
Having said all that: This is the worst possible time to carry out such experiments. There appear to be numerous bug reports concerning search right now and there is a lot of work done behind the scenes, which may or may not break the code I posted.  pecoes  00:21, March 30, 2012 (UTC) 
Your personal wiki has the same problem as the Cannabis Wiki for me. Try searching from the "advanced" link at w:c:pecoes:Special:Search there:
Search results and suggestions there open in the same tab for me. When you click the above link and do a search what happens?
Looking at all the JS pages there:
w:c:pecoes:Help:JavaScript and CSS Cheatsheet
I see the search JS here:
It is the same search JS as on the Cannabis Wiki: --Timeshifter 01:41, March 30, 2012 (UTC)

(Reset indent) AAAH! So that is the Advanced Search you were talking about. I have never seen that page before. See that's what you get, when you deal with a noob like me.

Yes, that's completely broken. I get no results whatsoever - with or without my code. It simply doesn't work.

That's a different search form, so you need to add a third ID:

$(function() {
    $('#WikiaSearch, #search, #powersearch').attr({ target: '_blank' });

Funny thing: While this Advanced Search thingy doesn't produce any results whatsoever, the search suggestions do work. It does suggest existing pages and when you click on them the results page does open in a new tab. It's the same empty results page though.

We should probably file a bug report for the Advanced Search, before we continue. --  pecoes  01:50, March 30, 2012 (UTC) 

Updated JavaScript

After adding the #powersearch JS, advanced search works fine on the Cannabis Wiki, even after changing my search preferences. All search results and suggestions open in new tabs!

Here is the updated JS:

/* Open search results in new tab */
/* */

$(function() {
    $('#WikiaSearch, #search, #powersearch').attr({ target: '_blank' });
WikiaSearchApp.initSuggest = function () {
    $.loadJQueryAutocomplete(function () {
            serviceUrl: wgServer + wgScript + "?action=ajax&rs=getLinkSuggest&format=json",
            onSelect: function (a, b) {
                WikiaSearchApp.trackInternal("search_start_suggest", {
                    sterm: encodeURIComponent(a.replace(/ /g, "_")),
                    rver: 0
      \$1/, encodeURIComponent(a.replace(/ /g, "_"))));
            appendTo: "#WikiaSearch",
            deferRequestBy: 250,
            maxHeight: 1000,
            selectedClass: "selected",
            width: "270px",
            skipBadQueries: true

I have checked Special:Search on Wikipedia and the Commons too. It also has this problem with advanced search opening in the same tab. Everything else opens in new tabs. Here is the version of the JS being used:

I will experiment with adding #powersearch to it, and talking further with the JS creator. You can too. See links higher up and here:

Site search via Firefox addon. Results in new tab

See the Firefox addon section here:

  • Search Site. Replaces the site search function of Google Toolbar. Google does not allow the toolbar on Firefox.
  • Site Search. Use with above addon. More options by using both. --Timeshifter 06:59, March 29, 2012 (UTC)

On a completely unrelated note

Does anybody else have this problem: I frequently - not always - get the message: "Welcome back. We've restored some of your previous changes... bla bla" and a completely blank page when I open the editor. Going back and forward a few times seems to "solve" the problem. Do you guys experience the same bug? --  pecoes  00:28, March 30, 2012 (UTC) 

It happens with the visual editor only, I believe. I have turned off the buggy visual editor in preferences. Until they allow the source editor tab to be on top by default, then I keep the visual editor disabled completely. Otherwise it causes innumerable problems.
The message you are seeing is part of yet another poorly-implemented "solution" from Wikia. :) They try to solve one problem, and introduce more problems. Then they ignore suggestions for improvement for months or years until even the most clueless exec at Wikia can't help but notice it. The problem they are trying to solve is what to do with uncompleted edits if I am remembering correctly. --Timeshifter 01:25, March 30, 2012 (UTC)
The message itself I've seen before. What is new and most likely the result of a bug, is that the page does not show my unsaved edit, but is completely blank instead. When I switch to the source view I get this:
<p />
That's all that's left of the page. I never tried to publish that and see what happens. I'll do that the next time. :) --  pecoes  01:57, March 30, 2012 (UTC) 
If the note is completely unrelated, it should really go in a completely new post. Like Timeshifter, I haven't used the Visual editor for many months because it was just too painful having it not default to Source mode. I used to use the Visual editor to see what those who used it were experiencing, but I decided the psychic pain it caused me was not worth the meager learning that using it provided. -- Fandyllic (talk · contr) 30 Mar 2012 5:08 PM Pacific

Wikipedia gadget

See: wikipedia:Wikipedia:Gadget/proposals#Open search results and suggestions in new tabs. --Timeshifter 03:02, April 6, 2012 (UTC)