Results 1 to 7 of 7

Thread: Ajax Content selection from URL?

  1. #1
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default Ajax Content selection from URL?

    I'm using the Dynamic Ajax Content script at http://www.dynamicdrive.com/dynamici...jaxcontent.htm and wondering if there is a way that a link to a page with this script can be made to automatically load one of the included pages. Right now, I have the links in a menu, but I want to select the dynamic ajax content through an external url. Is this possible at all?

  2. #2
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Do you mean load content from a domain that's not your own (ie: http://www.yahoo.com)? Unfortunately due to security reasons, Ajax doesn't allow you get content outside the domain the script is on. This is mentioned on the script page as well:

    Note: Due to security limitations, the external pages loaded must be from the same domain as the encompassing page. Any external .css and .js files associated with these pages, however, can be from any domain.

  3. #3
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Or do you mean that you link to page that will automatically load a url?

  4. #4
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    My thought was that if I have a lot of documents, instead of having them all accessable from a page menu, I could access them as needed from links in other pages as well by using a url something like this:
    Code:
    http://www.somesite.com/ajaxpage.html?file=nametoload&title=hotcontent
    I could even pass the title of the ajaxpage as a second variable, perhaps.

    Then I could use a parser to digest the url. Just like you were passing variables from one page to another. I did find some code that does this (untried and not implemented):
    Code:
     <script>
    function getQueryVariable(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i=0;i<vars.length;i++) 
        {
          var pair = vars[i].split("=");
          if (pair[0] == variable) {return pair[1];}
        } 
      alert('Query Variable ' + variable + ' not found');
    }
    </script>
    
    Now make a request to page.html?x=Hello
    
    <script>
      alert( getQueryVariable("x") );
    </script>
    And I also found:
    Code:
    function parsequery(){ 
      var arg_pairs = new Object(); 
      arg_pairs.names = new Array(); 
      arg_pairs.values = new Array(); 
      var query = location.search.substring(1); 
      if(query == "") { return null; } 
      var pairs = query.split("\&"); 
      for(var i = 0; i < pairs.length; i++)
        { 
          var pos = pairs[i].indexOf('='); 
          if(pos == -1) { continue; } 
          var arg_name = pairs[i].substring(0,pos); 
          var arg_value = pairs[i].substring(pos+1); 
          arg_pairs.names[i] = arg_name; 
          arg_pairs.values[i] = arg_value; 
        } 
      return arg_pairs; 
    }
    Then somehow I could use the returned variable pairs to give the filename to ajax to automatically display the contents (and perhaps the menu would be disabled). If nothing was returned, then the ajax page would be displayed in a sort-of default mode, permitting the use of the built-in menu. I suppose that this needs to be called in the onLoad event.....

    Another use would be to 'seed' the ajax page with selected content.
    Last edited by Strangeplant; 09-21-2006 at 01:19 PM. Reason: added function, corrected typo

  5. #5
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Code:
    function getQueryValue(name) {
        var match = (new RegExp('[?&;]' + name + '=([^&;#]*)')).exec(document.URL);
        return match ? unescape(match[1]) : null;
    }
    The above function from this thread is much better(I think) So, add this to the javascript
    Code:
    window.onload = loadstart;
    function getQueryValue(name) {
        var match = (new RegExp('[?&;]' + name + '=([^&;#]*)')).exec(document.URL);
        return match ? unescape(match[1]) : null;
    }
    function loadstart(){
    var page = getQueryValue('page');
    if(page != null){
    ajaxpage(page,'rightcolumn');
    //Change rightcolumn to whatever you called you div
    }
    }
    That should work.You use it like this.your urls should look like this
    Code:
    http://yourdomain.com/page.html?page=sompage.html

  6. #6
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    Thanks for your input and code. I was planning on working on it, and didn't know how direct it was. The code worked just great! I modified the function to provide for a default load or 'seed' of the page when accessed as standalone like this:
    Code:
    function loadstart(){
    var page = getQueryValue('page');
    if(page != null)
      {ajaxpage(page, 'contentarea');}
    else 
      {ajaxpage('./default.html', 'contentarea');}
    }
    This means that I can now do all sorts of things to access the content from other pages..........!
    Thanks again for your help, I really appreciate it.
    Last edited by Strangeplant; 09-22-2006 at 07:26 PM.

  7. #7
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    no problem

Bookmarks

Posting Permissions

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