Show popup when the browser closes - php

popup when the browser closes
I am using onbeforeunload event
<script>
function showPopup()
{
urlstring = "http://www.mydomain.com/popup.php";
window.open(urlstring,'mywin',"height=400px,width=500px,status=no,toolbar=no");
}
</script>
<body onbeforeunload="showPopup(); ">
but it also show popup when ever I hit back space and page refresh.
I want show popup only when browser close and not show when hit back space.
but it shows all conditions.
Please suggest me any other solution for this.

This is showing a popup every time because the onbeforeunload event is not aware of the domain you are leaving, it is only aware of the current page unloading for a new one.

Well , here's the bitter truth...
onbeforeunload is fired on page refresh and browser close.
The onbeforeunload event is fired every time the page is about to unload.
Which includes
Clicking on a link
Submitting a form
Closing the browser(or tab)
Refreshing the page
You might find this helpful
http://www.webdeveloper.com/forum/archive/index.php/t-36808.html

<script>
function showPopup(){
urlstring = "http://www.mydomain.com/popup.php";
window.open(urlstring,'mywin',"height=400px,width=500px,status=no,toolbar=no");
}
window.onunload= showPopup();
</script>
Note: This is DOM Level 0. Not part of any standard
EDITED: Note Infotekka's link to the document.onunload will also work appropriately for this problem.

You can't.
Your page, including its events, is not aware of other tabs, other pages or the browser itself for that matter. It also doesn't know why it is unloaded. The onunload event is the closest you will get, but it fired everytime when your page is unloaded, so also on navigation.

Related

Refresh only a div ("content") by universal button working on every page

To be clear - Ive already checked other Questions about refreshing div and the ideas I found were not exactly what I look for.
My site is made of plenty pages with the same header and footer (top, bottom, menu on both sides). I use smarty templates, and the Whole action of every page happens in one <div id="content">.
My users use to refresh most of those pages many times to do an action they've already done once again. With refreshing browser loads again header, footer, viewed page etc. I would like to bring them the button (instead of F5) which will refresh just a current content page (e.g. account.php) without refreshing whole site.
One of plenty structure:
<?php
$title = 'OneOfPlenty';
require_once("includes/head.php");
{
Whole action
}
require_once("includes/foot.php");
?>
header.tpl ends with <div id="content"> then comes
onofplenty.tpl and then in
footer.tpl I got </div> (close the content)
Here comes the question: Is it even possible? Am I able to create such a flexible button which will recognize which page is being displayed and will "know" to refresh just the content of this page?
Any ideas and help will be aprreciated.
Thank you
TTed
You could do an Ajax call with jQuery to get the output html of the tpl file of the page.
You could use an Ajax call, e.g. by using the jQuery get() function, e.g. like this
$.get("includes/account.php", function(data) {
$("#content").html(data);
alert("Load was performed.");
});
If you saved some kind of variable, either to session or to a data-content on your div. Just so you know which page you are on. Say if you are on account.php you set $('#content').attr("data-content", "account"). Then if you press the refresh button you could use an ajax get on $('#content').attr("data-content") + 'php' to re-import the data. Could be done with a SESSION variable as well.

Remove content of textarea when iframe is done?

So currently my website works like this; you post an update and through iframe your update gets added to the database and then shown in a list below. But the problem is that when you clicked "Submit" the text you wrote is still shown in the textarea because the website doesn't update completely. I have tried to have "onsubmit" and "onclick" but both remove the content of the textarea before it gets added to the database so it displays an empty message.
What should I do in order to delay it just a second or how do I make it wait for the iframe to "send" data to my PHP-script?
Give your textarea an id and supposing that iframe is an element of the page that contains the textarea you have done the following in the iframe page:
<?php
//code should be done after db add
?>
<script>
o = parent.document.getElementById('textareaID');
o.value = '';
</script>
<?php
//the end of code or something else
?>
You are able to see those demos on jsbin:
http://jsbin.com/ulOyiVo/1 The page with iframe. Supply the textarea with any text and then click on simulate submit link
http://jsbin.com/EyuBeLo/1/ The iframe page
If your only problem is to have a delayed response, you could trigger a setTimeout function to your onClick, with the given setTimeout:
setTimeout(
function() {
alert('hello');
},1250 //in milliseconds
);
You can define a click or a submit event using jQuery and send a request to your server. You can handle the event when the server responded using a callback. In that callback you need to do whatever it is needed to do. Using setTimeout in this case is an unnecessary hack. You will either set up a big time to wait harming the user experience or in case the page responds later than the specified time your page will work unexpectedly. So, instead of that try defining an event.

url change without anchor tag and refresh using ajax

As per my client need , redirect the page without anchor tag and refresh page ,
and change the URL as per page appearance. I don't know any idea about this.
I can use ajax for page content. but I don't know the URL change option without
page refresh or redirect. how can I do this with ajax j-query. Any one guide me for this issue.thanks advance
sample url
www.samplesite.com/contact.php -> without anchor tag. and page refresh this url need to work on php.
I think you are looking for info about the new HTML5 History API ( pushstate ), this link has a detailed tutorial.
http://diveintohtml5.info/history.html
You can do this using below function.
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
You can use the following javascript functions
window.location.assign('http://www.samplesite.com/contact.php'); // keeps the current page in browser history
window.location.replace('http://www.samplesite.com/contact.php'); // replaces the current page in browser history
window.location = 'http://www.samplesite.com/contact.php'; // the same as assign() and is backward compatible even with the oldest browsers
jsfiddle

Facebook like button not displaying after refreshing a div by using load();

I'm using a simple script to reload a div
$('#mydiv').fadeOut('300').load('# #mydiv').fadeIn("300");
The problem is that the div I'm reloading has the Facebook like button inside it. After the DIV reloads, I can see it updated inside the source, but the button is hidden for some reason.
Is there any way to force the button to re-draw?
As I stated in my comment, I think the .load is misunderstood, as you stated in your question
I can see it updated inside the source, but the button is hidden for some reason
.. so with that in mind, I assume you have load functioning with the correct parameters.
You have a synchronistic problem here. Everything you use in the chain uses a timescale, and .load() for that matter is asynchronous.
So instead of just chaining everything, you have to use the callbacks in order to know when the time scale ends for a particular function.
$('#myDiv').fadeIn('300', function() {
// callback when #myDiv is faded out (display:none;)
$(this).load('url', function() {
// callback when #myDiv is loaded with new content from the given 'url'
$(this).fadeIn('300');
})
});
The facebook button won't display because it is configured normally just AFTER document.load.
If you load the div content while the document is already loaded and the Facebook javascript SDK has already initialized. It won't work.
If facebook is not required UNTIL the div loads. You may just try to load the "all.js" script inside the div.
Otherwise, if you've come to that, you'll certainly have to review the application's design.

How to open multiple browser windows on request? (PHP)

So I have a form on PHP/HTML page. User submitss it to that same PHP/HTML page. So now PHP page I will have $_POST data. I want to when page is refreshed opnt some popUp browser windows which url's will be relative to users POST request. like www.example.com/bal-bla-bla.php? id=$_POST['StreamId']
Include some <script> elements with window.open calls in them in the response … then watch as every popup blocker in the world blocks them.
if ( isset($_POST['submit']) ) {
echo '<script>window.open ("'.$_SERVER['PHP_SELF'].'myplayer.php?stream_id='.$_POST['StreamId'].'","myplayer");</script>';
}
edited:
you can always display a message before open the window that advice the user to accept this new window!
var flag = confirm(" This window is not an ADV! ;-) ");
if (flag)
window.open("'.$_SERVER['PHP_SELF'].'","myplayer");
You would need to do this client side in javascript.
You could use window.open() in a document.onload event handler.
However, chances are if the user has a pop-up blocker this will be blocked.

Categories