Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    4
    Thanked 1 Time in 1 Post

    Exclamation AJAX Not Loading JAVASCRIPT

    Why don't AJAX load javascript from a different page? This annoys me so much because I have many pages I need to load using AJAX but won't work properly because AJAX wont load my javascript preset on the page, how can I fix this, or even get a alternative to?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    You can't use JavaScript to get content from other sites. JavaScript can only get content from the same site as the page containing the script.

    The Same Origin Policy (SOP) limits any AJAX XMLHTTPRequest browser call to URLs on the same server from which the host page was loaded. This means that it is impossible to make any kind of AJAX-style request to a different site than the one from which the page was loaded.

    "Good judgment comes from experience and experience comes from bad judgment."

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    4
    Thanked 1 Time in 1 Post
    There isn't any alternative?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by X888X View Post
    There isn't any alternative?
    Nope.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    Thanked 8 Times in 8 Posts
    If you find yourself wanting to do something that the tried and tested framework won't support (for good reasons), then it would suggest that you need to re-think what you're doing so that it does fit within the framework.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,441
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Well, you could use a proxy server on your own site that goes and makes the request of another site and then simply returns whatever that other site returns.

    Is it safe? No, not unless you apply some filtering to be sure that what is returned is harmless. Will it work? Yes.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    So many answers ... and I think that nobody understood the question. Probably because the OP was talking about "different page" which was mistaken as "different site".

    Nevertheless, standard XMLHttpRequest object methods do not execute the Javascript of the received content, whereas jQuery AJAX methods do. For XMLHttpRequest you'd either have to use eval() on your received content (not advised) or cut the script parts out of the content and create a new <script> element on your page with it.

    Generally I'd advise you to remove any Javascript code from your content before sending it. You should rather have all your Javascript on the current page (the one you start the Ajax request from) so that the loaded content is data only.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You can retrieve JavaScript from anywhere on the web using the following:

    Code:
    a = function(nm) {
      var s = document.createElement('script');
      s.type='text/javascript';
      s.src=nm;
      document.getElementsByTagName('body')[0].appendChild(s);
    }

    Then simply call a('http://example.com/script.js') to retrieve that remote script and run it.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

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