$.ajaxSetup in js vanilla - javascript

I have a jquery ajaxSetup which I want to convert in JS vanilla working cross browser.
So I need the same action (presetting data for all future jquery AJAX calls) written in JS vanilla.
$.ajaxSetup({
data: {
"_token": ajax_token
}
});
Which means I need this setup to be in JS vanilla, but I'll use jquery ajax after that (in rest of the code).
Have any idea? Thanks.

You can't.
The ajaxSetup method in jQuery works because when the various ajax methods in jQuery run, they check the place where ajaxSetup stores its data to see if they need to act other differently to the default.
ajaxSetup is the entry point that the jQuery library uses to store data in that place. It is internal to jQuery and you can't access it other than through that entry point.

Related

Knockout.js and Jquery Mobile

I have a Jquery Mobile and phonegap application. I use:
Jquery Mobile (1.3.1)
jquery (1.9.1)
Phonegap (2.8.0)
I have been asked to use knockout.js in the application. I am totally new to knockout.js and am still trying to understand how it works.
I will have to send ajax requests with JSON data and the responses will be JSON objects. I used to use for loops to handle the JSON objects and everything worked great. I'm just a little apprehensive on how to het all this to work with knockout.js.
Can anyone please give me suggestions on how to go about with this new knockout.js. I have very limited time (about 1 week), to understand how to get all this to work.
Or do you suggest sticking with what we know and use jquery and jquery mobile.
http://knockoutjs.com/documentation/json-data.html
Knockout doesn’t force you to use any one particular technique to load or save data.
You can use the one that you normally use
$.getJSON("/some/url", function(data) {
// Now use this data to update your view models,
// and Knockout will update your UI automatically
})
var data = /* Your data in JSON format - see below */;
$.post("/some/url", data, function(returnedData) {
// This callback is executed if the post was successful
})

Calling PHP scripts from Javascript without leaving current page

I'm having some trouble calling PHP scripts from Javascript without leaving the current HTML page (if it is at all possible). I understand it is possible using AJAX, although is it possible using Javascript alone?
Context:-
I want my page to perform a short animation using Javascript (using onclick), then immediately call a PHP script to insert data into a MySQL database - all without leaving the page so it does not inhibit the animation.
The animation part I can do and the inserting the data into the database, etc. but how can I call a PHP script at the end of that Javascript animation function?
Any pointers, code fragments, etc. would be greatly appreciated! ^_^
Apologies if this question has been asked previous.
AJAX is Asynchronous Javascript And XML,
Its a Javascript technology that allows you to send a request to the server (as your browser does when you enter a URL) and have the response in a javascript string instead of rendering it in the page.
The problem is different browsers do not implement AJAX the same way, So I suggest using jQuery for abstraction.
do this with jQuery:
<script>
$.get("backend.php",{param:value},callbackFunction);
callbackFunction(data)
{
alert(data);
}
</script>
Just happened to have the same issue, so I came up with something like that. All you have to do is add the code you need, and assign the do_the_script function to the onclick event.
<script type="text/javascript">
var myIntv;
function do_the_script() {
// play animation ...
var address='fancy_script.php';
var tmp = new XMLHttpRequest();
myIntv=setInterval(function(){
tmp.addEventListener("load", doneHandler, false);
tmp.open("POST", address);
tmp.send(null);
}, 1000);
}
function doneHandler(event) {
// maybe do something when script is executed
clearInterval(myIntv);
}
</script>
As you may have noticed, the code that "calls" the address is executed every 1 second. This, is to ensure that the call is made enough times so as to get a single positive answer, call the doneHandler and clear the interval afterwards. If you believe that your server can respond faster or slower you should change those milliseconds accordingly.
you can use jquery ajax:
http://api.jquery.com/jQuery.ajax/
PHP is a server-side language.
JavaScript is a client-side language.
If you want to execute server-side code, you don't have the choice to do a round-trip to the server. If you don't want to leave the page, your only option is doing an asynchronous request (aka AJAX).
Using a JavaScript library such as jQuery or MooTools greatly simplifies that kind of task. For example, you could use MooTools to do a request at the end of your script as such:
var req = new Request({url: '/backend/doPHPInsert.php'});
req.send();
There are ways to do so without AJAX by, for example, creating an iFrame dynamically (or any other element that fetches a resource).
I understand it is possible using AJAX, although is it possible using Javascript alone?
If you don't want to use XHR, you could use this ugly hack...
var request = 'mysql-insert.php',
image = new Image();
image.onload = function() {
// Success
}
image.onerror = function() {
// Error
}
image.src = request;
Except that was only really used before widespread use of AJAX (or needing to make a cross domain request).
I would just use AJAX. jQuery provides some great abstractions for working with XHR.
If you do not want to include the jquery library you can simple do the following
a) ad an iframe, size 0px so it is not visible, href is blank
b) execute this within your js code function
window.frames['iframename'].location.replace('http://....your . php');
This will execute the php script and you can for example make a database update...
This has some good examples for using unadulterated XMLHttpRequest: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
(Particularly, scroll down to the asynchronous examples - now you're cooking with gas.)
Edit: see also http://msdn.microsoft.com/en-us/library/ms535874%28v=vs.85%29.aspx which has examples for dealing with versions of IE which don't have native window.XMLHttpRequest.
Now, let's be frank - yes, working with XHR itself (especially cross-browser) is kind of obtuse. You can use just about anything out there (jQuery, Dojo, MooTools, Prototype, Closure, YUI, etc.) to do XHR more easily, because all of them, among other things, give you more concise XHR facilities.
It's still good to know what you're working with under the surface before you start letting a library do it for you though. :)

Importing external json files to a javascript script client-side without external libraries

I'm a bit new to javascript. Is there a way to do what I am describing in the title completely client-side and without any external libraries? Or is using jQuery the best/only way to go?
You can import a json file from a server via AJAX and them simply eval it. You don't need a library for that but using one makes it a lot easier. Of course just evaling a json string is not very secure as it can contain arbitrary text so all libraries parse it to see if it's well formed etc.
EDIT:
If you want to learn about AJAX you can start with this tutorial from w3schools. Ajax stands for Asynchronous Javascript And XML and it allows you to send a request to the server without reloading the whole page. In your case you will not be using Xml but JSON. Anyway, the tutorial explains the whole idea.
Yes there is. You can use the "document.write" to add scripts to the DOM at runtime:
in your case:
document.write('<script ...></script>');
Basically you are adding the script tag to the dom that will request the new file.
However there is something else to consider, although the script will be downloaded, you will need to have a variable assignment in it in order to use it in your page:
var x = { //json object };

Using jQuery to listen for an AJAX load that is not loaded using jQuery.AJAX

Okay, have a bit of a tricky one (for me anyway, i'm pretty rubbish at jQuery/JavaScript).
I'm pulling in data using standard AJAX (ie, NOT using a framework like jQuery or whatnot... there is a reason for it)
However, I then need to load up a jQuery script as soon as the page has been loaded in. So, here is the question, how do I bind the script once the DOM has been updated? I have been using Ariel Fleser's listen plugin (http://flesler.blogspot.com/2007/10/jquerylisten.html) for picking up on events such as clicks which works a treat, but I can't see how this can be used to listen for a load event.
Any ideas? I'm pretty stumped on this one!!
What's wrong with:
yourXMLHTTPRequest.onreadystatechange = function () {
if (this.readyState == 4) {
doJQueryStuff();
}
}
Also, what's the reason for not using the AJAX functionality provided in jQuery?
As an aside, it looks like that plugin you mention just replicates the functionality given by the live() and delegate() methods brought into jQuery in 1.4
In order to parse javascript loaded via ajax, you'll need to use eval(). Although, as far as these things go, a lot of coders consider eval() to be, um, evil... (a link, another link).
Regardless of the right and wrong of eval, this thread on webdeveloper should get you a lot of the way towards a solution.

What does this javascript do

window.$ = jQuery = $telerik.$;
To the best of my understanding, there is an Object called $telerik. It has a function $. The function is assigned to the identifiers jQuery and window.$, so we can just use $(args) in our javascripts. Is it correct?
It should mean that we can also use jQuery(args) to find an element.
The $telerik object is given by a set of commercial ASP .NET Web Controls called "RadControls for ASP .NET AJAX".
Recently they have included jQuery into the client-side API, and they decided to wrap the jQuery object inside $telerik for compatibility reasons.
The code you post is only declaring the $ and the jQuery objects globally, so you can reference to jQuery by it, and write code as you normally do.
For more info give a look to this post.
it just lets you use $telerik(''); as you would with $('') or jQuery(''); at least, so i believe

Categories

Resources