2

I'd like a script that could detect if called by jquery

If not, it'd display the page+layout, if not, it'd just so the content (but that is irrelevant)

But yeah, a way of detecting if it was called by jquery load - ajax - or directly requested.

Cheers!

Tom
  • 1,068
  • 2
  • 25
  • 39

4 Answers4

14

You might want to take a look at the HTTP headers your server is receiving.

For instance, let's consider I have this page :

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>
</head>
<body>
    <div id="test"></div>
    <script type="text/javascript">
        $('#test').load('temp.php');
    </script>
</body>
</html>

And the temp.php script contains only this :

<?php
var_dump($_SERVER);

die;


When load is executed, the "test" <div> will contain the dump of $_SERVER ; and it'll include this, amongst other things :

'HTTP_X_REQUESTED_WITH' => string 'XMLHttpRequest' (length=14)

XMLHttpRequest is the object that's used to make Ajax request.


This means you should be able to detect if the request was made via an AJax query, with something like this :

if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])
    && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
    echo "Ajax";
} else {
    echo "Not Ajax";
}

With this, you can detect whether your page is called "normally", or via an Ajax request, and decide if you must include layout or not.


BTW : this is exactly the solution that's used by, for instance, Zend Framework, to detect Ajax requests.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • First line can be simplified to if (@$_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') { – Waleed Amjad Sep 26 '09 at 20:29
  • 3
    @Sbm007 : I really don't like to "hide" errors with the @ operator, and use it only when it's trully necessary -- here, I want to test if that entry in $_SERVER is present and if its value is what I want, and this is exactly what isset and the second condition do. – Pascal MARTIN Sep 26 '09 at 20:32
  • Oh okay fair enough, I was unaware of that. I thought they were the same thing. Thanks. – Waleed Amjad Sep 26 '09 at 20:37
  • No need for isset nor the @ operator - `if($_SERVER['HTTP_X_REQUESTED_WITH']) { }` will do just fine. No need for the extra cruft. – ceejayoz Sep 26 '09 at 20:39
  • 3
    @Sbm007 : no problem :-) ;; @ceejayoz : this way, when your PHP script is called normally (ie, when that entry doesn't exist in $_SERVER), you'll get a "Notice: Undefined index: HTTP_X_REQUESTED_WITH" -- and yes, I always develop with notices activated, and I encourage everyone to do the same, on this one. – Pascal MARTIN Sep 26 '09 at 20:44
6

Could you not send a GET parameter with the load?

Ie:

jquery=1 //(for load)

jquery=2 //(for the 'low-level' ajax call)

Any other value for normal script load

Then you let the PHP script deal with determining what to do next. By reading the value of $_GET['jquery']

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Waleed Amjad
  • 6,716
  • 4
  • 35
  • 35
  • So if i have a script that says: $("#actualcontent").load($(this).attr("href")); how would I add an extra variable/GET parameter to this? – Tom Sep 26 '09 at 20:24
  • $("#actualcontent").load($(this).attr("href")+'?jquery=1'); – Waleed Amjad Sep 26 '09 at 20:26
  • If overly manual. jQuery already sends something along with the request you can use without having to change a single line of JS. – ceejayoz Sep 26 '09 at 20:37
  • 3
    I also vote for using `$_SERVER['HTTP_X_REQUESTED_WITH']` like the other answer. After all, that's exactly what it's there for -- to see if the request was from an Ajax implementation. – grantwparks Sep 26 '09 at 22:54
  • Fair enough, but how will you differentiate from a jQuery load() call and jQuery ajax() call? As mentioned in the question. – Waleed Amjad Sep 26 '09 at 23:03
  • I don't think you're reading the question the same way. I don't see a need to distinguish between ajax() and load() in the question. You're reading it as "jQuery load, ajax, or direct" - I'm reading it as "jQuery load (i.e. ajax) or direct". – ceejayoz Sep 27 '09 at 13:57
6

If I'm remembering correctly, jQuery's AJAX functions send a X-Requested-With header (with a value of XMLHttpRequest).

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
1

if you're using Zend Framework you could use

// in one of your controllers
if ($this->getRequest()->isXmlHttpRequest()) {
  // ...
}

http://framework.zend.com/manual/en/zend.controller.request.html

Svetoslav Marinov
  • 1,498
  • 14
  • 11