I have a JSP page that refreshes every 5 seconds Using ajax.
The page i am calling having javascript that is not getting refreshed .
Please tell me how to achieve that.
Below is the code i am using to refresh that page .
refresh is the name of the div where i am displaying the data.
<script type="text/javascript">
function AutoRefresh() {
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
} catch (e) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("No AJAX");
return false;
}
}
}
xmlHttp.onreadystatechange = function () {
//alert("hi");
if (xmlHttp.readyState == 4) {
document.getElementById('TotalRoutes').innerHTML = xmlHttp.responseText;
setTimeout('AutoRefresh()', 10 * 1000); // JavaScript function calls AutoRefresh() every 3 seconds
}
}
xmlHttp.open("GET", "QAGENIE.jsp", true);
xmlHttp.send(null);
}
</script>
Here js files in the QAGENIE.jsp page is not getting refreshed on the ajax call
I think you are not calling AutoRefresh function first time. so nothing will be execute. and if you are calling from outside of your code which you gave here, then please post whole web page code in question.
setTimeout(AutoRefresh,5000); for every 5 seconds
if (xmlhttp.readyState==4 && xmlhttp.status==200)
window.onload = AutoRefresh();
When you call document.getElementById('TotalRoutes').innerHTML=xmlHttp.responseText;, script tags inside the "QAGENIE.jsp" are not executed, that's why:
js files in the QAGENIE.jsp page is not getting refreshed.
Since the question is tagged with jQuery, you could try $.ajax and $.html. Like this:
function AutoRefresh(){
$.ajax({
url:"QAGENIE.jsp"
})
.done(function(response){
$("#TotalRoutes").html(response);
setTimeout(AutoRefresh,10*1000);
});
}
AutoRefresh(); //call this to trigger the first call
$.html automatically parses and executes script tags in the response HTML.
There is 1 more thing to notice is browser cache, if your js files are cacheable (as it usually does by the cache response header from server), the js files may be served from the cached. If you need to always refresh with new js files, ensure your js files are not returned with a cache header, or try to append a random string at the end of the script tags. Like this:
<script src="yourFile.js?[randomstring]"></script>
Related
I have one url to download content i want to hit that url in the backend to start downloading and forwad the response to the aany other jsp page.
When i write window.location.href(download_full_url) its working fine only to download the content but side by side i want to forwared the respose to another jsp page.
<script>
function yes(content_name,previous_page) {
//downloadContent(content_name);
window.location.href = "download_full_url";
backPage(previous_page);
}
</script>
To download the content in the backend process, i have also made one js function to hit the url without waiting to get the response but its not working here is my another js function.
function downloadContent(content_name) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","download_full_url?"+content_name="+content_name, false);
xmlhttp.send();
}
When i call abovefunction instead of window.location.href=download url its not working its call the backpage() after calling downloadContent function the response to previous page.
<script>
function backpage(previous_page) {
window.location.href = previous_page;
}
</script>
Hope this is an nice easy one
I want to show a notify when an ajax function completes. My ajax is fine I just can't seem to get the notify to work. I have jquery and everything installed and I also have notify.js from http://notifyjs.com/.
Using the google developer tools I just get the error below all the time.
Uncaught TypeError: $.notify is not a function
Here is my ajax function. This works just fine exactly as I need just no notify is displayed :(
function MyAjaxFunction()
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("mainpagemenu_myDiv").innerHTML = xmlhttp.responseText;
$.notify("Hello World"); // <- **** this is the thing I want to do
}
}
xmlhttp.open("POST", "/myaspfile.aspx", true);
xmlhttp.send();
}
I have linked in my files
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
and
<script src="/bootstrap/js/notify.js"></script>
Link to the following script
<script src="http://notifyjs.com/dist/notify-combined.min.js"></script>
at the top of the page (before using the Ajax function)
I'm counting clicks on a link by calling a page with AJAX but in Firefox, apparently because the page called is never actually loaded, for some reason Firefox never calls it. It seems like the link is clicked and then Firefox makes the AJAX call but somehow because the page changes to the actual link in the href then the AJAX call is never actually sent (appears red in firebug and no sign of it in Fiddler). It works fine in IE & Chrome and if I change the link to target="_new" then it will work in Firefox. Am I making some sort of stupid mistake?
<HTML>
<HEAD>
<script type="text/javascript">
function adtrk(cde){
var ajaxRequest; // The variable that makes Ajax possible!
var r=Math.random();
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaresp = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "/atr.php?cde=" + cde + "&r=" + r, true);
ajaxRequest.send(null);
}
</script>
</HEAD>
<BODY>
<a onclick="adtrk('zip1'); return true;" href="http://www.google.com"><img src="/images/img.jpg"></a>
</BODY>
</HTML>
When you load a new page any open AJAX requests will be cancelled (by the browser). Your server probably never sees the click count request.
Either always open links in a new window or use some other mechanism for counting clicks, like a proxy/redirect.
The page changes before the ajax request is done. You should wait for you ajax request to end before changing page by running it synchronously :
ajaxRequest.open("GET", "/atr.php?cde=" + cde + "&r=" + r, false);
I am using ajax in my web page.I want to access a variable used in java script function (in head section of html) by a jsp page.Jsp page is retrieving data from database using that variable.
How can i do that?
Please help me.
if you need the call data you will set & or have a var that's empty before hand. what I realized is that ajax localizes even declared new var's the best way i've found is to append to a existing. Now with the new string and or literal array / object
<script>
function s(e){
alert(e);
}
var a = '';
//Jquery Version
$.get('test.php',function(data){
a += data;
s(a);
});
// Javascript Version
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
a += ajaxRequest.responseText;
s(a);
}
}
ajaxRequest.open("GET", "test.php" , true);
ajaxRequest.send(null);
}
$(function(){
ajaxFunction();
});
//-->
</script>
You can do it by adding new jsp file, flow will be like this :
Create new jsp file like databaseOperation.jsp which will contain your code for retrieving database record.
Through javascript function, call databaseOperation.jsp file and pass your javascript varible.
Access this variable in your jsp file and retrieve the required code from DB.
You can simply do this by making a ajax call and passing that variable with ajax request
<script>
var id=1;
$.get("yourpage?id="+id,function(){
//get this id serverside using `get`
})
</script>
You have to change your approach to get this done. We cannot set javascript variable value to the the database code which is written in the jsp file. This is because, the database code will be rendered from the serverside and only the html will be sent to the client.
You can achieve this by having a Controller(Servlet) with this database code with asynchronous support and in your jsp file, call this controller through javascript ajax and manipulate the HTML DOM for your requirement.
I'm attempting to use AJAX to insert data into a database, but am unable to connect to the .php file on the server end. I'm thinking this is due to the fact that this is all run on Wordpress, so my normal url path to the .php file might not be correct.
Here is my setup:
Template file:
$('#newsletter-register').submit(function(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
alert(ajaxRequest.responseText);
}
}
ajaxRequest.open("GET", "register_newsletter.php", true);
ajaxRequest.send(null);
});
'#newsletter-register' is form that the user will use to submit his/her information. The alert when the ajaxRequest is at readyState 4 is thrown correctly, but will an empty value.
My php is a simple echo returning a string to signal that the connection is correct (presently I'm just trying to see if the files are correctly calling eachother).
I'm thinking it must be the url path to register_newsletter.php as I've tried placing the code within the template file, outside, etc.
My javascript file is located in /theme/assets/js/code.js where as my template files (including the register_newsletter.php file) in /theme.
Any ideas?
I had this type of issue yesterday. In my situation the page would only be accessed from
www.site.com/aFolder/
The script was stored in my
'/wp-content/themes/themeName/processForm.php'
folder.
and the URL I used was:
../wp-content/themes/themeName/processForm.php
My JS was simply in the head of the main header.php file, but note that the URL I had to use was relative to the URL the page the script was accessed from.
Does that shed any light on things?
EDIT:
As troubleshooting steps, try ensuring you can access your php file from it's absolute URL, and have a look in firebug to see what link is being called when you fire the ajax request to see if it matches up with what you expect.