Question

I don't know how to ask this question that's why I'm going to simulate what I need, some developers make what I need by rendering javascript with php. A simple sample file would look like:

my_javascript_file.php

<?php include "my_class.php"; ?>

$(document).ready(function(){    

  $.ajax({
    url: "<?php $my_obj->post_url(); ?>",
    success: function(){
      alert("Post successful")
    }
  });
};    

which outputs following:

$(document).ready(function(){    

  $.ajax({
    url: "/post_handler.php",
    success: function(){
      alert("Post successful")
    }
  });
};    

my question comes there, is there any way to achieve that with pure js techniques?

Thanks, Sinan.

P.S. This is just a simple example of course I can hardcode post url here, or get url by location object etc with javascript I hope you get what I mean...

EDIT: Sorry for confusions by the terms I used, it is just getting some information from my application and writing to my javascript dynamically. I don't mean comet or data encode etc.

What I want to know is how to use server-side data on javascript files(if there is any way). Like what php method does on my example, it simply echoes a string which comes from my application. Is there a way to do that by a javascript library or a framework, like a template engine you assign vars and use it in that template system. Hope it's more clear now.

Was it helpful?

Solution

Yes there is a way, but its odd. You can use the jaxer server. It lets you run javascript on the server.

EDIT:

As I said in my comment I don't think you want to be downloading extra js to the client in order to implement a templating engine. Its probably a separation or decoupling that you're talking about. In which case you could have the following in a separate js file:

var myObject = {
var1 : '',
var2 : 0,
init : function(options) {
    var1 = options.var1;
    var2 = options.var2;

    $('#someElem').click(function() {
        // do stuff
        return false;
    };
}

};

and then call it like this from your html page

<head>
<script type="text/javascript">
    var options = {
        var1 : <?php echo "hello" ?>,
        var2 : <?php echo 1 ?>
    }
    myObject.init(options);
...

OTHER TIPS

i am not sure i know what you mean by "push" ... if it is a real server push, you should look at comet servers ...

also, to me it's unclear what "pure js techniques" are ... you can do this without jQuery of course ... but pure JS, as the languages primarily designed to manipulate the DOM will not suffice of course ... you need AJAX-like approaches ... however there was a proposal for push in HTML5, but yeah, this is gonna take some time ...

so i hope this helps ... but maybe you should clarify your question a little, i am sure you will get better answers ... :)

greetz

back2dos

You could ask the server for the URL using .get or .ajax (assuming your urls.php will handle this properly and return a proper string - or JSON, in which case you might as well use .getJSON - with the URL in it). For example (untested of course):

$.get("urls.php", { operation: "post_handler" },
    function(data){
        $.ajax({
            url: data,
            data: eventData,
            success: function(){
                alert("Post successful");
            }
        });
    });

================= Edit: New Potential Answer =====================

Say you manage to retrieve JavaScript Data Objects from the server, do you wish to inject the JavaScript data you obtained in your HTML markup, client-side?

If that's the case you might look at IBDOM: http://ibdom.sf.net/

I wrote it, we've had it in production on a number of a sites for a couple of years.

================= Old Answer Below ====================

So you want "eventData" to be "something" which can be consumed by JavaScript.

I'd recommend first taking a look at JSON in PHP:

[REMOVED link to JSON in PHP, not needed]

Then your PHP Code might resemble this:

<?php include "my_class.php"; ?>

$(document).ready(function(){    

  $.ajax({
    url: "<?php $my_obj->post_url(); ?>",
    data: <?php json_encode ($my_obj->getData()); ?>,
    success: function(){
      alert("Post successful")
    }
  });
};

So the idea here, is that the value of your "data:" field would become a JSON Object graph generated by PHP on the server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top