Jump to content

AJAX call externally


9three

Recommended Posts

Hey,

 

I've seen some sites that do the following:

 

<a href="example.php" onclick="ajax_functionname(parameter)">Example</a>

 

Which then that function calls an external file and does what it's suppose to do.

 

I'm currently using jQuery and every example is done inline. Which means people can view the source, how it functions, what page gets requested, and most importantly it means I have to go to every page with that same function and edit it page by page.

 

For example, in one of my pages I do this:

 

$.ajax({
  url: "test.html",
  cache: false,
  success: function(html){
    $("#results").append(html);
  }
});

 

I know that I could do $('.ajax').onclick(function () {...}) but it still shows what's happening.

 

How can I accomplish using this style instead?

Link to comment
Share on other sites

function getPage(page) {

    $.ajax({

        url: page,

        cache: false,

        success: function(html){

            $("#results").append(html);

        }

});

 

}

 

 

<a href="blah.php" onclick="getPage('blah.php'); return false;">blah.php</a>

Link to comment
Share on other sites

hm.. maybe that wasn't the best example.

 

I'll use facebook.com as an example.

 

When you click on the button "sign up" it calls an ajax function. You can't see the file it's being referenced though. Here's the link:

 

onclick="seo_tracking_onclick(this,"facebookpoc","facebookpoc","1","1","Signup","Simple Reg Box");RegUtil.getInstance().ajax_validate_data({ignore: ['captcha']}, 'registration_container', '1' ); return false;"

 

That's from the not logged in index page. In the html source there is no reference towards an outside file, although I know there is. It's sorta like "hidden".

Link to comment
Share on other sites

Here's an even better example, from newartist.com

 

In the head, they have:

 

xajax_chkLogin = function() { return xajax.request( { xjxfun: 'chkLogin' }, { parameters: arguments } ); };

 

 

And in the body

<input id='submit' type='button' name='submit' value='Login' class='submit-button' onclick='xajax_chkLogin(xajax.getFormValues("login"));' />

 

Again no reference to an external file.

Link to comment
Share on other sites

Here's an even better example, from newartist.com

 

In the head, they have:

 

xajax_chkLogin = function() { return xajax.request( { xjxfun: 'chkLogin' }, { parameters: arguments } ); };

 

 

And in the body

<input id='submit' type='button' name='submit' value='Login' class='submit-button' onclick='xajax_chkLogin(xajax.getFormValues("login"));' />

 

Again no reference to an external file.

 

I would imagine the xajax.request method defaults to the current file (since it makes a request to index.php).

 

 

Oh, actually it's set here:

 

xajax.config.requestURI = "http://newartist.com/index.php";
xajax.config.statusMessages = false;
xajax.config.waitCursor = true;
xajax.config.version = "xajax 0.5";
xajax.config.legacy = false;
xajax.config.defaultMode = "asynchronous";
xajax.config.defaultMethod = "POST";

 

 

 

 

Unless you want to use some client side parsing to determine the URL you need to make a request to based on the current URL or something, there's not really a way to magically know what page to make the request to.

 

 

Edit:  One option could be to do it all through the same page and have different handlers.  For example if handler=Something, then a function called Something could be executed (or a method of a class called Something).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.