 |

11-12-2007, 08:20 PM
|
|
WebProWorld Member
|
|
Join Date: Jan 2006
Location: USA
Posts: 41
|
|
AJAX and javascript calling javascript
I am using (or attempting to use, anyway) an AJAX solution for use in posting client-side information to update database values on my website. I have the AJAX portion working and calling a page to process the database update, but I'm having problems getting the values from the client-side javascript to use in my local AJAX javascript function.
For instance, I run website www.aaa.com, and within my site, there is a call to a javascript at an external website, bbb.com which I use to display certain information within my site.
So, I call bbb.com's javascript as such:
Code:
<script type="text/javascript"
src="http://www.bbb.com/jsfunction.js?a=30&key=deadbeef"></script>
Now, I want to take the return value (an integer ranging from 0-1000) from the above javascript call and place use it in a variable within my local javascript. Can this be done?
Something like:
Code:
<script type="text/javascript">
var key = "<?= $item_key ?>";
var xyz = document.write("<script type='text/javascript'
src='http://www.bbb.com/jsfunction.js?a=30&key="'+key+'"><\/script>');
ajaxFunction('update', xyz);
</script>
Any pointers you all can give would be greatly appreciated.
Thanks.
Last edited by krnl : 11-12-2007 at 08:46 PM.
|

11-13-2007, 10:54 AM
|
 |
Moderator
|
|
Join Date: Jun 2006
Location: United States
Posts: 1,843
|
|
Re: AJAX and javascript calling javascript
If the javascript you are calling is a function that returns a value, you should be able to capture the return value the same as you would in a local function call.
HTML Code:
<!-- Include the Javascript containing the target function -->
<script type="text/javascript" src="http://remoteserver/remote.js"></script>
<script type="text/javascript">
// Set the variable to be passed to the remote function
var key="x";
// Call the remote function, and store the return value in another variable
var value = remoteFunction(key);
</script>
And for those of you that are wondering, yes, to bbb.com, this would be an exploit of a cross site scripting vulnerability. Javascript bad. 
__________________
The best way to learn anything, is to question everything.
Last edited by wige : 11-13-2007 at 10:56 AM.
|

11-13-2007, 12:13 PM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: May 2005
Location: Norway
Posts: 5,402
|
|
Re: AJAX and javascript calling javascript
What makes this AJAX? Where is the asynchronous communication with the server? A time now since I looked at AJAX, but can you use the XMLHttpRequest Object that is used for asynchronous communication with the server?
Also note that DOM is intimately integrated with the X (XML) part of AJAX. Very much can be done with DOM functions and more specifically with DOM XML functions.
No need to reinvent the wheel.
Last edited by kgun : 11-13-2007 at 12:19 PM.
|

11-13-2007, 01:03 PM
|
|
WebProWorld Member
|
|
Join Date: Jan 2006
Location: USA
Posts: 41
|
|
Re: AJAX and javascript calling javascript
The ajax function is defined earlier in the file and uses the xmlhttprequest mechanism to call a local php script that will take care of the database update. The call to the ajax function follows the javascript stuff with ajaxFunction('update', var1, var2); where var1 is the key and var2 is the output from the remote javascript call.
Code:
function ajaxFunction(action,var1,var2) {
http.open('get', 'rpc.php?action='+action+'&var1='+var1+'&var2='+var2);
http.onreadystatechange = handleResponse;
http.send(null);
}
I am getting everything to pass correctly to the AJAX function except the previously noted remote javascript call. The remote javascript outputs the following: document.write('32'); - or whatever number...I need that number for var2 of my AJAX function call.
If I'm reinventing the wheel and going about this all wrong, then please tell me what the proper approach is.
Thanks for your feedback! I look forward to hearing more!
|

11-13-2007, 01:19 PM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: May 2005
Location: Norway
Posts: 5,402
|
|
Re: AJAX and javascript calling javascript
As I told you, I am a bit away from AJAX now, but note that Google combines Remote Scripting with IFRAME with the XMLHttpRequest object as far as I know to get the best combination of both worlds.
I am not sure that that solves your problem though, and I can not, as I told be more specific now. Will take me some hours to repeat that stuff.
|

11-13-2007, 01:51 PM
|
|
WebProWorld Member
|
|
Join Date: Jan 2006
Location: USA
Posts: 41
|
|
Re: AJAX and javascript calling javascript
kgun, I do appreciate your input. I'll keep reading with a couple of the links that are provided in your post.
If anyone else has any thoughts on how to accomplish my objective, please let me know!
Thanks!
|

11-13-2007, 01:58 PM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: May 2005
Location: Norway
Posts: 5,402
|
|
Re: AJAX and javascript calling javascript
I know where I would have looked. In one of the following to books that I have here: - Professional AJAX NCZOnline - The Official Web Site of Nicholas C. Zakas
- Building Responsive Web Applications. AJAX and PHP. Christian Daire et al.
|

11-15-2007, 06:30 PM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: Aug 2003
Location: Edmonton, AB, Canada
Posts: 3,406
|
|
Re: AJAX and javascript calling javascript
[url="http://www.devshed.com/c/a/Security/A-Quick-Look-at-Cross-Site-Scripting/1/"] A Quick Look at Cross Site Scripting - What is Cross Site Scripting?[/ur]
BTW, there is a way to serve php with ajax, but I havent found it yet, but for now, this site is excellent, here are some:
[url="http://www.devshed.com/c/a/PHP/Creating-AJAX-Requester-Objects-with-Abstract-Factory-Classes-in-PHP-5/"]Creating AJAX Requester Objects with Abstract Factory Classes in PHP 5 [/ur]
[url="http://www.devshed.com/c/a/PHP/PHP-Datastorage-Class/1/"] PHP Datastorage Class[/ur]
Before we get into the nitty-gritty of XML parsing with PHP, I'd like to take some time to explain how all the pieces fit together.
[url="http://www.devshed.com/c/a/XML/Using-PHP-with-XML-part-1/1/"]Using PHP with XML [/ur]
[url="http://www.devshed.com/c/a/PHP/PHP-and-JavaScript-Interaction-Storing-Data-in-the-Client-part-1/"]PHP and JavaScript Interaction: Storing Data in the Client [/ur]
[url="http://www.devshed.com/c/a/PHP/Creating-AJAX-Requester-Objects-with-Abstract-Factory-Classes-in-PHP-5/1/"] Creating AJAX Requester Objects with Abstract Factory Classes in PHP 5 - Working with AJAX HTTP requester objects[/ur]
This guy is excellent, I mean excellent. Ho has a tute on serving php dynamically with javascript, etc..
[url="http://www.devshed.com/cp/bio/Alejandro-Gervasio/"]Alejandro Gervasio [/ur] but I cannot find them. I have on my back-ups somewhere, and will get.
I also used PEARL libraries to do what you want to do. I have been out for two years, but I know it has been done, don't need to re-invent! I did it with ajax and xml as well as MySQL db when dooing google maps programming.
Basically, you can put ajax into a <div> and read and write there dynamically without re=generating your whole page.
More specifically, in this second article of the series, I'm going to demonstrate how to apply the abstract factory pattern to create diverse AJAX HTTP requester objects, which can be used in distinct contexts.
[url="http://www.devshed.com/c/a/PHP/Creating-AJAX-Requester-Objects-with-Abstract-Factory-Classes-in-PHP-5/"]Creating AJAX Requester Objects with Abstract Factory Classes in PHP 5 [/ur]
I'll be back later with a staright forward solution... Basically parsing js and extracting the var, then using echo to write the js withthe var included.
(WTF?)
__________________
What I am is what I am, are you what you are, or what.
Eddie Brickel
Last edited by mikmik : 11-15-2007 at 06:34 PM.
|

11-16-2007, 10:34 AM
|
 |
WebProWorld Veteran
|
|
Join Date: Dec 2006
Location: Calgary, Alberta, Canada
Posts: 389
|
|
Re: AJAX and javascript calling javascript
Why would you want to cross call a Javascript file from another site? Why not just load the file onto your site and avoid all the problems?
|

11-16-2007, 11:01 AM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: May 2005
Location: Norway
Posts: 5,402
|
|
Re: AJAX and javascript calling javascript
"Cross server scripting", a potential security risk or am I wrong when done by the webmaster him/her self?
zXml 1.0.2
A JavaScript library for cross-browser XML, XPath, and XSLT support. (24 KB)
One of the best JS libraries for AJAX I know off. There are other on the page:
NCZOnline - Downloads
Last edited by kgun : 11-16-2007 at 11:06 AM.
|

11-16-2007, 12:17 PM
|
|
WebProWorld Member
|
|
Join Date: Jan 2006
Location: USA
Posts: 41
|
|
Re: AJAX and javascript calling javascript
Quote:
Originally Posted by DaveSawers
Why would you want to cross call a Javascript file from another site? Why not just load the file onto your site and avoid all the problems?
|
The script on the other site queries their database and provides me with the output from that query. It's just how they did it. I would like to essentially sync my database value with theirs using the output that is available.
I haven't had a chance to work on this for the last couple of days, but I do appreciate the replies that I have received and will be visiting them for information when I get back into this.
|

11-16-2007, 12:31 PM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: May 2005
Location: Norway
Posts: 5,402
|
|
Re: AJAX and javascript calling javascript
Quote:
Originally Posted by krnl
I would like to essentially sync my database value with theirs using the output that is available.
|
That is the main reason that you connect to another site. There is a fairly advanced OO soution to this described in Volume II of Harry Fuechs book "Object oriented PHP solutions" with the accompanying SPLIB library.
May be
Davey Shafik, Ligaya Turmelle, Harry Fuecks, Matthew Weier O'Phinney & Ben Balbo (2007) " The PHP Anthology: 101 Essential Tips, Tricks & Hacks, 2nd Edition" that I have just ordered is the updated version.
As you can see, there is also a code archive with that book that you get access to once you have bought the book.
I think there is a database class there that eases the connection to more than one database at the same time. If I remeber correct, it is explained how you can access MySQL in one part of your application and another database like PostgreSQL: The world's most advanced open source database in another part of the application.
Last edited by kgun : 11-16-2007 at 12:36 PM.
|

11-18-2007, 02:54 AM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: Aug 2003
Location: Edmonton, AB, Canada
Posts: 3,406
|
|
Re: AJAX and javascript calling javascript
Quote:
Originally Posted by krnl
The script on the other site queries their database and provides me with the output from that query. It's just how they did it. I would like to essentially sync my database value with theirs using the output that is available.
I haven't had a chance to work on this for the last couple of days, but I do appreciate the replies that I have received and will be visiting them for information when I get back into this.
|
You said you are getting a js from the other site. This is not a db query, you can easily query an SQL DB remotely, but js?
I don't like this any more, and I wont help you.
What kind of script queries a DB? I am curious about this, because you need a server-side language, not a scripted language, to make a DB connection.
This is fishy.
(asp not included, <-_0>
__________________
What I am is what I am, are you what you are, or what.
Eddie Brickel
Last edited by mikmik : 11-18-2007 at 02:58 AM.
|

11-18-2007, 10:48 AM
|
|
WebProWorld Member
|
|
Join Date: Jan 2006
Location: USA
Posts: 41
|
|
Re: AJAX and javascript calling javascript
Perhaps I misspoke. The database query is performed by an ASP page (I know nothing about ASP...I'm a PHP kinda guy). The output of the query is provided to subscribers of the service via this remote javascript function.
You can call it fishy all you want.
I think I'm giving up on this approach anyway. The developer on the other site is trying to whip up something that's a little more user-friendly on subscriber side of things.
Thanks for all the great input everyone. Onward and upward...
|

11-18-2007, 11:00 AM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: May 2005
Location: Norway
Posts: 5,402
|
|
Re: AJAX and javascript calling javascript
There are ASP to PHP translators and PHP to ASP as well.
<? PHP CODE in ASP ?> - IIS.net
Note the date of the post. There may be better updated solutions, and the right place to ask should be the Forums - IIS.net
PHP4Mono
Kw search
Exact: "php to asp translation"
Free: php to asp translation
Other:
php to asp compiler
php to asp processor
It is not an ideal solution, but it is one fast ad hoc solution if it can be used.
If you buy the book I mentioned above, I think the name of the connection class (where the database connection is encapsulated - $db=& new MySQL($host,$dbUser,$dbPass,$dbName); ) is MySQL included in the file MySQL.PHP that followed with the code archive to the old books (volume I and II).
Old version of MySQL.php used (Is there a more up to date version in the new book?)
OOP in PHP and MySQL tips: Start here.
Some of the links may be broken since I moved the code to a server with PHP 5.2.* and have had no time to update it.
Note: PHP 5.2.5 Released For some PHP projects, like XML parsing you may need that version.
Last edited by kgun : 11-18-2007 at 11:29 AM.
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|