Submit Your Article Forum Rules

Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: AJAX and javascript calling javascript

  1. #1

    Arrow 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.
    Rick - Linux Tutorials for Beginners
    allin-enterprises.com | VaGunForum.net | all-in-general.com | MagliteSales.com |

  2. #2
    WebProWorld MVP wige's Avatar
    Join Date
    Jun 2006
    Posts
    3,138

    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.
    WigeDev - Freelance web and software development

  3. #3
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    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.

  4. #4

    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!
    Rick - Linux Tutorials for Beginners
    allin-enterprises.com | VaGunForum.net | all-in-general.com | MagliteSales.com |

  5. #5
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    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.

  6. #6

    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!
    Rick - Linux Tutorials for Beginners
    allin-enterprises.com | VaGunForum.net | all-in-general.com | MagliteSales.com |

  7. #7
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    Re: AJAX and javascript calling javascript

    I know where I would have looked. In one of the following to books that I have here:
    1. Professional AJAX NCZOnline - The Official Web Site of Nicholas C. Zakas
    2. Building Responsive Web Applications. AJAX and PHP. Christian Daire et al.

  8. #8
    WebProWorld MVP mikmik's Avatar
    Join Date
    Aug 2003
    Posts
    1,557

    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?)
    Babies don't need a vacation, but I still see them at the beach... it pisses me off! I'll go over to a little baby and say 'What are you doing here? You haven't worked a day in your life!'
    Steven Wright

  9. #9
    WebProWorld MVP DaveSawers's Avatar
    Join Date
    Dec 2006
    Location
    Lunenburg, Nova Scotia, Canada
    Posts
    760

    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?
    Dynamic Software Development
    www.activeminds.ca

  10. #10
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    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

Page 1 of 2 12 LastLast

Similar Threads

  1. jQuery, Ajax, JavaScript
    By Mark.M in forum Services for Sale/Hire
    Replies: 4
    Last Post: 05-26-2009, 12:33 PM
  2. JavaScript Remote Scripting: Building an AJAX-based Random
    By WPW_Feedbot in forum Graphics & Design Discussion Forum
    Replies: 0
    Last Post: 10-20-2005, 01:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •