How to close google chrome at a particular event using Javascript/jQuery? - javascript

I am using the following JS/JQuery code in order to close the browser at a particular event.
It works perfectly on Internet explorer but not sure why its not working on Google Chrome.
<div class="divRowButton">
<asp:LinkButton ID="btnContinue" runat="server" CssClass="button buttonText" Width="205px"
Height="33px" CausesValidation="false" OnClientClick ="return CloseBrowser();"></asp:LinkButton>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
//IE6 Fix, expand the content to full height
if (jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 6) {
$divH = $("#secHeader");
$divN = $("#secNav");
$divF = $("#secFooter");
var ht = $(window).height() - $divH.height() - $divN.height() - $divF.height();
$(".divContent").height(ht - 10);
}
});
function CloseBrowser() {
window.open('', '_self', '');
window.close();
}
</script>
</asp:Content>
I changed my above code to the following below code but still it didn't work for chrome. I am wondering what changes do I need to make in the below code so that it works perfectly for chrome.
<div class="divRowButton">
<asp:LinkButton ID="btnContinue" runat="server" CssClass="button buttonText" Width="205px"
Height="33px" CausesValidation="false" OnClientClick ="return CloseBrowser('quit');"></asp:LinkButton>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
//IE6 Fix, expand the content to full height
if (jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 6) {
$divH = $("#secHeader");
$divN = $("#secNav");
$divF = $("#secFooter");
var ht = $(window).height() - $divH.height() - $divN.height() - $divF.height();
$(".divContent").height(ht - 10);
}
});
function CloseBrowser(cmd) {
if (cmd == 'quit') {
open(location, '_self').close();
}
return false;
}
</script>
</asp:Content>
The following image will give more idea what changes I did in both of the above codes.

If you have a problem with some functionality, start simple:
<body>
<script>
window.close();
</script>
</body>
It works just perfect on every browser.
If you would like to close the window that you previosly opened:
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_close
From the question itself, I am not able to determine what you really want to achive? Would you like the window to be closed on some event?

I examined your closeBrowser function and rewrote it:
<body>
<script>
var cmd = prompt("Please enter cmd");
if (cmd == 'quit') {
    window.close();
}
</script>
</body>
It is also based on 'cmd'. The value of cmd is taken out of the user prompt.
In addition, your first version of code is fine. You just need to invoke the function after creating it:
function CloseBrowser() {
window.open('', '_self', ''); window.close();
}
CloseBrowser();
Now it works on chrome as well. Using this syntax, you can avoid prompt before closing the window. To see the efect, use setTimeout function:
setTimeout(CloseBrowser, 5000);
It will close the window after 5 seconds.

Related

Dynamic content is not displayed on page reload

After my page is loaded, I run a script where I dynamically add click event to my hidden button, and trigger the click. This way I am forcing a javascript library to reload every time a reload a page.
Then this library makes request, and response is being displayed in appropriate manner.
First time I load the page, the response is displayed (selected text is highlighted). Every next time, library is reloaded, but response is not displayed. I want to make it displayed every time I reload the page.
I have tried to disable cache, refresh it with shift+f5, but still no success.
This is my code:
<body>
<div id="content">
<div id="pdfContainer" class = "pdf-content">
</div>
</div>
<input id="btn" type="button" hidden="true" />
<script>
function loadAnnotator() {
var content = $('#content').annotator();
console.log("Annotator loaded");
};
$(window).load(function () {
var btn = $("#btn");
if(btn !== null && btn !== undefined) {
btn.on('click', loadAnnotator);
btn.click();
}
});
</script>
</body>
I can make it work with displaying and manually clicking this button, but that is not the desired behavior.
How can I make it work the way I desire? Thank you in advance.
Instead of the whole hack, try this oneliner instead:
$($('#content').annotator));
There's no need for the hidden button. If this doesn't work there's a chance 'annotator' isn't initialised until after this script.
The problem is with the btn.click(); this doesn't work in some browsers!
JavaScript to rescue like this btn[0].click(); or you need to change your logic as follows
<body>
<div id="content">
<div id="pdfContainer" class = "pdf-content">
</div>
</div>
<input id="btn" type="button" hidden="true" />
<script>
var content =null;
function loadAnnotator() {
if(content==null)content =$('#content').annotator();
console.log("Annotator loaded");
};
function clickedButton(){
loadAnnotator();
}
$(window).onLoad(function () {
var btn = $("#btn");
if(btn !== null && btn !== undefined) {
btn.on('click', clickedButton);
clickedButton();
}
});
</script>
</body>
Solved: I have added a timer and forced the button to be clicked after 300ms.
Somehow like this:
<body>
<div id="content">
<div id="pdfContainer" class = "pdf-content">
</div>
</div>
<input id="btn" type="button" hidden="true" onclick="loadAnnotator()" />
<script>
var content = null;
function loadAnnotator() {
if (content === null) {
content = $('#content').annotator();
}
console.log("Annotator loaded");
}
;
function clickedButton() {
var btn = $("#btn")[0];
if (btn !== null && btn !== undefined) {
btn.click();
console.log("Button is clicked");
}
}
$(window).load(function () {
setTimeout(clickedButton, 300);
});
</script>
</body>
Also, I've listened to suggestions as
var btn = $("#btn")[0];
Sometimes, one forgets about such fundamental things about jQuery :)
If I add time less then 300ms, it is behaving the way I mentioned above.
Thank you, all of you!

close .aspx using JavaScript/ Jquery

I have searched this issue on other links like window.close and self.close do not close the window in Chrome etc. but couldn't able to solve it
I want to close a page using below code, but it's not working on any browser.
my url is like :
http://server/solutionName/Confirmation.aspx
Warning :
Scripts may close only the windows that were opened by it.
HTML:
<img alt="Close" src="images/close.png" onclick="return confirm_delete();">
Script:
function confirm_delete()
{
if (confirm("Are you sure you want to close?") == true)
window.close();
else
return false;
}
Here is the screenshot:
Try the following lines of code
var Browser = navigator.appName;
var indexB = Browser.indexOf('Explorer');
if (indexB > 0) {
var indexV = navigator.userAgent.indexOf('MSIE') + 5;
var Version = navigator.userAgent.substring(indexV, indexV + 1);
if (Version >= 7) {
window.open('', '_self', '');
window.close();
}
else if (Version == 6) {
window.opener = null;
window.close();
}
else {
window.opener = '';
window.close();
}
}
else {
window.close();
}
window.close When this method is called, the referenced window is closed.
This method is only allowed to be called for windows that were opened
by a script using the window.open() method. If the window was not
opened by a script, the following error appears in the JavaScript
Console: Scripts may not close windows that were not opened by script.
https://developer.mozilla.org/en-US/docs/Web/API/Window.close
The hacky workarounds for Chrome (reopen window inside itself and close it):
<script>
open(location, '_self').close();
</script>
This code works in my Chrome:
<script>
function confirm_delete()
{
if (confirm("Are you sure you want to close?") == true) open(location, '_self').close();
else return false;
}
confirm_delete();
</script>

How to give option to recall function using javascript after running on load

I want to automatically run a function upon loading the webpage, and then I want to give the option to rerun the function after it's executed.
The following code will work if I the part is not in the database, and will work if instead of using document.write is use alert for displaying the part, but if I use document.write, the button to search again disappears. I think it's because the buttons aren't being re-initialized. I've moved that around and gotten nothing, and tried reloading the webpage, which is functional, but unsatisfactory because of the time it takes.
Is there anyway to prevent the buttons from disappearing, or a better method you recommend?
Thanks in advance.
<!DOCTYPE HTML>
<html>
<body>
<input id="clickMe" type="button" value="Search for Another Part" onclick="search_part();" />
<script type="text/javascript">
function search_part()
{
var part = prompt("Stackoverflow example: ");
if( typeof part === 'undefined' )
{
alert("That part is not in the database.")
}
else
{
document.write( part )
}
}
window.onload = search_part();
document.getElementById("Button").onclick = search_part;
</script>
After DOM is loaded, document.write replaces all content on page. Instead you probably want to have a element container on body and display messages in that.
<input id="clickMe" type="button" value="Search for Another Part">
<div id="messages-here"></div>
<script type="text/javascript">
function search_part () {
var part = prompt("Stackoverflow example: ");
if (typeof part === 'undefined') {
alert("That part is not in the database.");
} else {
document.getElementById('messages-here').innerHTML = part;
}
}
window.onload = search_part();
document.getElementById("clickMe").onclick = search_part;
</script>

Closing RadWindow using RadButton

I'm trying to use a RadButton to close a radwindow from with the window itself (via javascript). Is it possible to call a script to close the window? Here is the javascript:
function getRadWindow()
{
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}
function closeWindow()
{
getRadWindow().close();
}
And here is the button:
<telerik:RadButton ID="CancelButton" runat="server" OnClick="closeWindow();" CssClass="clicker" Text="Cancel" UseSubmitBehavior="False" EnableEmbeddedScripts="false" CausesValidation="False" RegisterWithScriptManager="False">
</telerik:RadButton>
I have tried everything, the script will only work if I use a pure HTML element such as an anchor tag. If I use the OnClick event I get the following error when the window opens: Compiler Error Message: CS1026: ) expected.
Am I missing something?
Thanks!
I'm not sure if I am improving this answer, I'm just trying to make it easier to understand. I have a rad window that is opened from a main page. The radwindow is opened in Code Behind (C#), not Javascript. When my user clicks a Save button on the RadWindow, it performs some logic tasks, then it closes the radwindow itself. You simply need to:
Put thise code block in you RadWindow aspx.....
<telerik:RadCodeBlock runat="server" ID="rcb1">
<script language="javascript" type="text/javascript">
function GetRadWindow()
{
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}
function CloseDialog(button)
{
GetRadWindow().close();
}
</script>
</telerik:RadCodeBlock>
Put this code in your RadWindow's button click after you perform your pre-close logic (the same button that performs the other logic closes the window)
C#
ClientScript.RegisterStartupScript(typeof(string), "", "CloseDialog();");
OR
VB
ClientScript.RegisterStartupScript(Me.GetType(), "", "CloseDialog();")
If you're wondering how to open the radwindow from codebehind here is how I did it:
RadWindow window1 = new RadWindow();
// Set the window properties
window1.NavigateUrl = "winStrengthChart.aspx?EMPLOYIDNAME=" + parmString;
window1.ID = "RadWindow1";
window1.Width = 800;
window1.Height = 650;
window1.VisibleStatusbar = false;
window1.Behaviors = Telerik.Web.UI.WindowBehaviors.Close | Telerik.Web.UI.WindowBehaviors.Resize | Telerik.Web.UI.WindowBehaviors.Move;
window1.VisibleOnPageLoad = true; // Set this property to True for showing window from code
rwm1.Windows.Add(window1);
this.Form1.Controls.Add(window1);
...AND of course you need a basic RadWindowManager on the main page that opens the window:
<telerik:RadWindowManager ID="rwm1" runat="server">
<Windows>
</Windows>
</telerik:RadWindowManager>
This should work if I have made a mistake please correct me.
Thanks
The way to call a function from the RadButton is by using either its OnClientClicked or OnClientClicking event. Then you need to pass only the name of the JavaScript function, without parenthese. OnClick is a property for the server handler, this is also the case with the regular asp button. Try this:
<telerik:RadButton ID="CancelButton" runat="server" OnClientClicked="closeWindow" AutoPostBack="false" CssClass="clicker" Text="Cancel" UseSubmitBehavior="False" EnableEmbeddedScripts="false" CausesValidation="False" RegisterWithScriptManager="False">
Note the AutoPostBack property is set to false to prevent the postback.

dynamically set iframe src

I have a program which will dynamically set an iframe src to load pages. I need to hook a event handler for the page completely loaded. How can i do it? Thanks!
<script type="text/javascript">
function iframeDidLoad() {
alert('Done');
}
function newSite() {
var sites = ['http://getprismatic.com',
'http://gizmodo.com/',
'http://lifehacker.com/']
document.getElementById('myIframe').src = sites[Math.floor(Math.random() * sites.length)];
}
</script>
<input type="button" value="Change site" onClick="newSite()" />
<iframe id="myIframe" src="http://getprismatic.com/" onLoad="iframeDidLoad();"></iframe>
Example at http://jsfiddle.net/MALuP/
Try this:
top.document.getElementById('AppFrame').setAttribute("src",fullPath);
Try this...
function urlChange(url) {
var site = url+'?toolbar=0&navpanes=0&scrollbar=0';
document.getElementById('iFrameName').src = site;
}
TEST
You should also consider that in some Opera versions onload is fired several times and add some hooks:
// fixing Opera 9.26, 10.00
if (doc.readyState && doc.readyState != 'complete') {
// Opera fires load event multiple times
// Even when the DOM is not ready yet
// this fix should not affect other browsers
return;
}
// fixing Opera 9.64
if (doc.body && doc.body.innerHTML == "false") {
// In Opera 9.64 event was fired second time
// when body.innerHTML changed from false
// to server response approx. after 1 sec
return;
}
Code borrowed from Ajax Upload
try this code. then 'formId' div can set the image.
$('#formId').append('<iframe style="width: 100%;height: 500px" src="/document_path/name.jpg"' +
'title="description"> </iframe> ');
Try this:
document.frames["myiframe"].onload = function(){
alert("Hello World");
}

Categories