Close firefox tab with Javascript - firefox

(Disclaimer) I have read threads that this can't be done, and this can be done so i'm kind of confuse weather it can or not. These threads were also somewhat old so i'm asking again to see if the answer has been found by a different audience. If i annoy someone by asking this question again, i truly apologize. This was never my intention.
I have a website that sends you an email to confirm your information. When you click the link in the email, a new tab opens with a form that has your information filled out and some check boxes to agree or disagree to the terms. Once you check all the requirements, there is a button at the bottom that says agree. Right now if you click it, it submits the page, resets the fields you clicked on and i think this is a bit confusing to the end user.
I was wanting to close the page for them once they click the agree button. so now i have a jQuery dialog box pop up asking if they want to stay or leave the page. if they click leave the page, it will close that tab for them. This works in IE(surprisingly), Chrome, Safari, and Opera. Firefox is another story.
Can someone please help me or tell me if this can be done in Firefox?
This was my referance. http://www.yournewdesigner.com/css-experiments/javascript-window-close-firefox.html
And here is the code i have:
$(document).ready(function () {
$("##agree").dialog({
autoOpen: false,
resizable: false,
height:230,
width:400,
modal: true,
buttons: {
"Leave this page": function() {
$("##VerificationForm").submit();//nothing to do with closing tab
open(location, '_parent','').close();
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
$("##agreebtn").click(
function checkAndSubmit(action) {
if(validateForm()&&validateAgreement(action)) {//nothing to do with closing tab
$("##agree").dialog('open');
return false;
}
}
);
});

The threads stating that it can be done were right at one point, but they aren't now. FireFox has patched that exploit out.
You cannot close a tab or window in FireFox that your script didn't open in the first place.
You can go into your FireFox:config and change the values around to your liking, but for security reasons, it's defaulted to "false".

Related

workaround to firefox creating new history after each document.write()

in Firefox after each document.write() new history entrie is made, so if document.write() is called on page load when user presses back button to go back in history firefox takes him to the current page which runs document.write() again and creates another history so even another back button pressing won't help and take him where he wants
simple workaround is:
function onbeforeunload(e)
{
setTimeout(function() { window.history.go(-2); }, 0);
}
window.addEventListener("beforeunload", onbeforeunload, true);
but then it won't let user go any link or type any address into addressbar and go there, so is there another way to fix firefox?
a better way would be to use onpopstate event instead but it does not work in firefox after document.write()
and no, document.write() must be used here.
Use document.open("text/html", "replace") instead of just document.open().
Works like a charm.
Interesting to note that other workarounds did NOT work - such as $('html').replaceWith(pageContent) or manipulating the history if the browser is Firefox...
Use document.open to run document.write while keeping the history unchanged:
<script>
document.open("text/html",document.write("foo") );
</script>
References
DOM document.open

mobiscroll - selecting option on click

I'm in the process of deciding how to implement "selects" on a site, and am strongly considering mobiscroll, there's just one little thing that's annoying me, and that's the "extra click" (compared to an old-fashioned dropdown) of the "set" button that's required after choosing an option.
My question is, is there a way to close the scroller and set the value when an option is chosen via click or touch, but not if a new option is "chosen" because of dragging the scroller or using the mousewheel?
This is my code:
<script type="text/javascript">
$('#myScroller').mobiscroll({
preset: 'select',
theme: 'android-ics',
mode: 'mixed',
display: 'bubble',
rows: 7,
inputClass: 'subtleDropdown',
onChange: function(val, inst) {
//I would think this is where it's supposed to go, no luck thus far though!
console.log(this);
},
});
</script>
Cheers!
In the end, we decided not to use mobiScroll (too little info on it available), so I'm closing this question.

Why does an ASP.NET DropDownList control requires two clicks to expand in Internet Explorer

I have an ASP.NET DropDownList control that renders into a dropdown list (select HTML tag) on a page. For some reason, when I'm in Internet Explorer, it requires two clicks for me to open it up and see the options, which is just an extra click for the end-user. It works fine in Google Chrome, Mozilla Firefox and Safari--I only have to click once to see the options for a selection. Why would it not work correctly in IE? And more importantly, how can I fix it in IE?
Here is my code:
<asp:DropDownList id="DDLClientName" runat="server" EnableViewState="False" AutoPostBack="True" class="InputField" onfocus="Change(this, event)" onblur="Change(this, event)">
Had to remove the hard-coded onfocus event. IE handles the first click for the focus event, and the second to expand the dropdown. I guess this is a known quirk with IE along with the other 400+ quirks.
I am still trying to figure out a way to change styles of the dropdown on focus. Depending on what code you put into this callback anonymous function, you may still need to click the dropdown twice in IE. I've found that you can monkey with other controls, inside this function and it doesn't require two clicks. I'll keep this as the answer for now. I guess because of Microsoft we can't use the onfocus at all on dropdowns. I may try using an actual select tag rather than using Microsoft's ASP.NET DropDownList, and see if I can use the onfocus event then, without the extra click. I doubt it.
jQuery(this.Elements.DDLClientName).focus(function() {
.. put code here
});
I had this same problem and this is due to how IE 10 handles onFocus, it treats the first focus as a click. What I did to fix it was bind the mousedown event to the click event. Then, you can run whatever code you need in the click event.
// if IE 10
if (navigator.userAgent.indexOf("MSIE 10") > 0)
{
$("#InvoiceTypeDropDown").bind('mousedown',function(event) {
$(this).trigger('click')
});
}
So my complete code looked like this:
if (navigator.userAgent.indexOf("MSIE 10") > 0)
{
$("#InvoiceTypeDropDown").bind('mousedown',function(event) {
$(this).trigger('click')
});
$("#InvoiceTypeDropDown").click(function () {
if ($(this).val() == '') {
$(this).css("color", "black");
$(this).css("font-style", "normal");
}
});
}

S5 Accordion Menu ( Mootools) changes

I'm using S5 Accordion Menu on my Joomla site.
http://jalousie.al-soft.ru/o-programme
What I need is to make it not slide down, when I reload page. It needs to work like accordion only when you click on it items, but not when the page reloads.
However it will be great, if it will be possible to save its open state for current page, but without accordion effect when page loads, just load it opened.
Sorry for my english. Let me know if you have any ideas.
Here is the source
http://jalousie.al-soft.ru/modules/mod_s5_accordion_menu/js/s5_accordion_menu.js
if you use the Accordion class that ships with mootols/more/FX just use the initialDisplayFx option to disable the initial animation. Something like the following code should work.
var s5_accordion_menu = new Accordion($('s5_accordion_menu'),
'h3.s5_am_toggler',
'div.s5_accordion_menu_element', {
opacity: true,
allowMultipleOpen: true,
display: s5_am_openElement,
alwaysHide: true,
initialDisplayFx: false
});
The signature of the class you use does not match the "official" one but maybe it is just a wrapper otherwise the answer is not, you can't disable the effect

Safari 5 Extension: How can I detect when a window's current tab has changed?

I have a Safari 5 extension that contains a toolbar. Whenever the current tab changes, that toolbar should be updated. I would like to do something like this from my bar's script:
safari.self.browserWindow.addEventListener("activeTab", tabChanged, false);
However, that doesn't seem to work. I have tried a number of other event names as well:
activeTab
activeTabChanged
onActiveTab
onActiveTabChanged
tab
tabChanged
onTab
onTabChanged
selectionChanged
onSelectionChanged
Does anybody know how to detect when the active tab changes?
Not that this is in any way related, but it looks like I would do this in Chrome with:
chrome.tabs.onSelectionChanged.addListener(tabChanged);
Safari 5.1 has several new events for extensions, including an "activate" event that fires when a window or tab is focused.
https://developer.apple.com/documentation/safariextensions/safariactivateevent
That's the event you are looking for. I'm not sure, but i think is a new addition to the extensions api. You can put in global.html or in the popover.html
safari.application.addEventListener("activate", activateHandler, true);
function activateHandler(event) {
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage('someId', false);
}
I agree with #imiaou 's answer: from looking at Apple's docs there doesn't seem to be a way to do this :(.
Since I needed to detect tab changes for my extension (which I'm porting over from Chrome), I did the following polling-based workaround which seems to be working fine (in my global page):
var prevActiveTab;
setInterval("poorMansOnTabChange()", 1500); //poll every 1.5 sec
function poorMansOnTabChange() {
var curTab = safari.application.activeBrowserWindow.activeTab;
if (curTab != prevActiveTab) {
prevActiveTab= curTab;
console.log("active tab changed!");
//do work here
}
}
I'm unhappy with constantly polling the browser, but I see no other way around this until Apple adds support for these tab-events. If your extension can live with a relatively relaxed tab-switch event trigger latency then this could be a reasonable workaround for now (1.5 sec. max latency is acceptable for my extension, and it doesn't feel like its slowing down the browser).
While Safari doesn't have Chrome's specific tab-related API, it does have a perfect solution to this problem.
#Galt was 99% of the way there, with the idea to add an event listener to your injected JavaScript and to dispatchMessage that information to your extension.
The event handler you're looking for is named focus, and gets fired every time a tab or window gets selected.
In your injected code:
var tabInFocus = function( event )
{
safari.self.tab.dispatchMessage("tabFocusSwitched","");
}
window.addEventListener("focus", tabInFocus, false);
You can then update your extension's UI, with the data relevant to safari.application.activeBrowserWindow.activeTab
I found this method works better than focus event, it can be managed in the background page:
safari.application.addEventListener("validate", PopUp.validateCommand, false);
var PopUp = {
activeTab : null,
// commands are validated before being excecuted
validateCommand : function(aEvent) {
// this is a hack for detecting tab switches, safari does not have a dedicated API like Chrome
if(PopUp.activeTab !== null){
if(PopUp.activeTab !== safari.application.activeBrowserWindow.activeTab){
$.each(safari.application.browserWindows, function(aIndex, aWindow) {
$.each(aWindow.tabs, function(aIndex, aTab) {
// message all tabs about the focus switch event
if (aTab !== safari.application.activeBrowserWindow.activeTab && aTab.page) {
aTab.page.dispatchMessage("tabUnfocused");
}else{
aTab.page.dispatchMessage("tabFocused");
}
});
});
}
}
// set the new active tab
PopUp.activeTab = safari.application.activeBrowserWindow.activeTab;
}
}
This code will help to trace the change in URL :-
Write this code Inject.js , in side function
function trackURL() {
alert("beforeNavigate "+safari.application.activeBrowserWindow.activeTab.url);
setTimeout(function() {
alert("afterNavigate "+safari.application.activeBrowserWindow.activeTab.url);
}, 500);
}
safari.application.addEventListener("beforeNavigate", trackURL, true);
It seems Apple doesn't provide much API for us manipulating tabs like Chrome does.
Currently, there is no way to detect tab event.
Unlike chrome which provides a special API for events like window and tab changes, you can still do it with safari extensions.
You simply have to have your injected javascript set up event listeners for the events that you want.
Then if that info is needed by global or other parts of the extension, you can pass the info in messages using the postMessage command.
injected.js:
window.addEventListener("load", loaded, false);
safari.self.tab.dispatchMessage("somethinghappened","load");

Resources