When I tried to use it and append data to a div, it works fine.
With that said, I was wondering if it can be used to simulate, for instance, a php include file. Such as:
var id= $(“#field_id”).val();
load(“defined_url.php?id=id”);
no appending it to a div for display, just good old fashion “include the contents of this php file, with a querystring appended to it”
Is that possible?
I know that I can do this:
$('#div_name).load(url);
and load it under div id=“div_name”
I don’t want that. I just want to 100% simulate the php include concept if possible since my web host doesn’t allow the dynamic querystring include files to be used.
You might want to know why but trust me when I tell you it’s way too complicated to get into
You can pass a URL with a query string to jQuery’s load() function. Be aware that this will not work the same way as PHP’s include() because the content loaded will be the HTML that PHP generates when it parses the file on the server, not a PHP script that can be evaluated.
How does this “include” functionality work? Is what you “include” an HTML string? If that’s the case, it sounds a lot like document.write(), which does not belong in this decade. Otherwise, it’s impossible, because JavaScript needs to know where to insert this stuff, which is what you apparently already know how to do:
Thanks for the replies. I will address some of the comments.
Raffles>>Makes sense. What I want to do is take json output from a page (the querystring lets the external page determine what db data to pull before it prints it out)
My jquery functions work when the json loads within a jquery variable and being able to
use an include concept works for this. If I hard code json within this var things work great but my site requires a more dynamic approach.
The issue with the ‘add to div’ concept is that the ‘div’ concept doesn’t really work with my unique project.
Having a true 'just include the json output within the variable, such as
var xyz {
json data
}
is ideal because, should this happen, the functionality of the site will work perfectly. Therefore, I was looking for a true ‘just include the output, nothing special’ solution. None of this would be needed if the web host would allow dynamic include files via php
Are you wanting to declare the JavaScript object extracted from the JSON string in your page so that other scripts in the page can access it? There is a way to load JavaScript on demand that may work for you. This scenario requires that the “included” file be 100% JavaScript. Since JSON is nothing more than a serialized Object literal, it will be evaluated as if you had declared an Object literal.
function jsInclude(url)
{
var head = document.getElementsByTagName('HEAD')[0];
var script = document.createElement('SCRIPT');
script.type = "text/javascript";
script.src = url;
head.appendChild(script);
}
jsInclude("http://mysite.com/some_url?with=lots&of=query&string=args");
This will load and execute an external JavaScript file on demand. If the script contains an Object literal (i.e., JSON text), that object will be declared in global scope.
could I use that function between the q_cdata: [ and the ] ; (thereby replacing the above json data or must I put all of that syntax in the php file and just load up the function?
Thanks for this incredible tip. The only reason I ask is because trying different ‘output options’ I have had some poor results as this is still a bit new to me.
PS. What I am doing is trying to correct a well made system that has one flaw. Long story short,
having json hard coded during onload, in theory, makes the system work well, but the problem is that
it’s based on multiple forms per page and, for now, it only loads json from any random form, not based on a form that offers user input.
Therefore, being able to load that external file’s json and simulating the ‘onload echoed output of json’ is ideal for this unique system. 99/100 times I’d go for getjson/etc, but I’ve learned that this ‘odd request’ works best for this project. (for now)