how to run script onbeforeunload - php

hi somebody pls explain to me why this code of mine doesn't work
$(document).ready(function()
{wireUpEvents();
});
function wireUpEvents()
{window.onbeforeunload = function()
{
window.location="link"; // on before unload i will run this link first.
}
}
when i refresh my page this code will run but it'll not go to link(window.location) before load however when i add alert like this
window.location="link"; alert("anything");
it'll go to my link before load. I like to eliminate that weird alert().
pls feel free to suggest any treatment with my code. thanks

try below code
window.onbeforeunload=testfunc;
function testfunc()
{
window.location="link";
}

Try this :
<script type="text/javascript">
$(window).bind('beforeunload', function() {
window.location="link";
}
);
</script>

Related

I cant fetch the data using ajax i dont know why

is any one can help how to fix this one... I already inserted , but if i select nothing happens..
You must call display function after page load!
In script
(function($) {
function disply(){
/*YOUR DISPLAY CODE*/
};
$(function() {
disply();
});
}(jQuery));

Getting output of PHP script using JQuery

I am trying to load the output of a PHP script into a using JavaScript and JQuery. The JavaScript function I am using uses the $.get function in JQuery to call a php script, which I want to display in another division. The script I wrote is:
<script type="text/javascript">
function on_load() {
$(document).ready(function() {
alert('here');
$.get("http://localhost/dbtest.php", function(data){
alert('here too too');
$("uname").html(data);
});
});
}
</script>
The PHP script (dbtest.php) uses a simple echo statement:
echo "hello, world!!!";
I am getting the first alert here, but not the second. What Can I be doing wrong here?
I suppose uname is a ID, in that case you should use:
$("#uname").html(data);
You can add this to your php for debugging:
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1);
Try also to remove http:// from your ajax call and use relative path instead.
The guys below pointed out that the selector is wrong. Indeed that's a problem, but I think that the real issue is that you don't get the second alert. Probably your php file localhost/dbtest.php is not accessible. What happen if you open localhost/dbtest.php in a new tab?
I think the problem is the path to your dbtest.php file. You are saying you seecond alert will not be displayed so your request must be wrong.
Try to copy your page into the same folder like dbtest.php open the page in your browser with http://localhost/yourfile.php
If this does not display both alert-boxes try the same with an open developer console (Chrome/IE = F12) and look if there are errors.
You say you are trying to make ajax requests to your localhost from a Phonegap application. Phonegap prevents making ajax requests to other domains by default. You must add localhost to whitelist. Here is more detail: http://docs.phonegap.com/en/1.9.0/guide_whitelist_index.md.html
Here you have a working example:
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<div id="uname"></div>
<script>
function on_load() {
$(document).ready(function() {
alert('here');
$.get("http://localhost/dbtest.php", function(data){
alert('here too too');
$("#uname").html(data);
});
});
}
on_load();
</script>
Beware of the same-origin-policy. The page loading dbtest.php must be from the same origin, unless you grant other origins by adding a header from dbtest.php.
Try add some error handling to your code to better see what´s happening.
<script type="text/javascript">
function on_load() {
$(document).ready(function() {
alert('here');
$.get("http://localhost/dbtest.php", function(data){
alert('here too too');
$("uname").html(data);
}).fail(function () {
alert("failed");
});
});
}
</script>

Can't find what's wrong in this code

I have the following script which brings up a dialog on my screen telling me there has been an error:
<script type="text/javascript">
var myFunc = function()
{
var html='<div class="cs-body">explanation of the error</div>';
$(html).csDialog();
return false;
};
</script>
I want this dialog to pop-up when my php script returns a "1" value for $error, like this:
if ($error==1) {
echo "<script type='javascript'>$(document).ready(myFunc)</script>";
}
Even if I leave the if-clause and just echo the script it doensn't do anything. Can somebody tell me what I'm missing here?
Thanks in advance!
try changing script type from 'javascript' to 'text/javascript'
that did the trick! thanks lostsource!
You need to make sure, this script fragment is output after the inclusion of jQuery.
Check if you javascript function is defined when running this
echo "<script type='javascript'>$(document).ready(myFunc)</script>
Include jQuery and your js function in the tag, just to see if it works.
PS: you should only add js script in the tag if they are necessary right away, else, added them at the bottom of the tag. Remember, the loading of js files blocks the loading of any other page resources
try this
<?php if ($error==1) { ?>
<script type="text/javascript">
$(document).ready(myFunc)
</script>";
<?php } ?>
Dins

Problem in jquery enter key event

I need a little of your help here.
In my case I am having search textbox on b.php, in which user can enter username and hit enter to get the serched user-details.
Ok this is so far. Now my search code to deal with database is ready in c.php and i want to call it through b.php with jquery's event.
actuaully my a.php is home file and calling b.php through it on click event. Now flow for jquery will be like this a.php > b.php > c.php
There is no button to search, hitting enter key only will give searched user.
I writ my code in jquery as:
$(document).ready(function(){
$('#srchtxt').bind('click', function(){
if($('#srchtxt').val() != '') {
$('#loading').html('<img src="images/ajax-loader(1).gif">');
$('#loading').show();
$.get('/usersearch.php?tnm3='+arr3, '', function(data){
$('#content').html(data);
$('#loading').hide();
});
}
});
$('#srchtxt').bind('keyup', function(e){
if(e.keyCode==13) {
$("#srchtxt").trigger('click');
}
});
});
This event is not working. can you help me here?
I think you mixed search field and search button.
The proper solution is folowing:
$('#srchtxt').keyup(function(e){
if(e.keyCode==13) {
$("#search-submit").trigger('click');
}
});
$('#search-submit').click(function(){alert('button has been clicked')});
See working example here
It is not working both on Firefox and IE.
For ie i think we have to use the following code to get the event
if(window.event)
{
window.event.keyCode
}
Instead of .bind try to use .live() http://api.jquery.com/live/
I usually use event.which instead of event.keyCode
It may be compatible with more browsers.
Plus, you mixed up some ID...

Automatic href Link click

Hai
i want to generated an automated click event. I am working in php server, i Know Javascript.
Below is my Code
<script language="javascript">
function autoClick() {
var elm=document.getElementById('thisLink');
elm.click();
document.getElementById('thisLink').click();
}
</script>
</head>
i put this on inside the body tag :
onload="setTimeout('autoClick();',3000);"
and inside the a tag :
href="./apage.php" id="thisLink" name="thisLink" target="newWindow"
But it doesn't work in MOzilla Is any solution , 0r any other solution ???
Thanks in advance
You could try JQuery's trigger function.
$('#thisLink').trigger('click');
This should possibly work although I haven't tested it.
JQuery: http://jquery.com
doc: http://docs.jquery.com/Events/trigger#eventdata
Element.click works only on input elements on Mozilla. Try something like
function autoClick() {
var elm=document.getElementById('thisLink');
document.location.href = elm.href;
}
instead, or if you prefer opening the link into a new window,
function autoClick() {
var elm=document.getElementById('thisLink');
window.open(elm.href, 'autoclickwindow');
}

Categories