PDA

View Full Version : Ajax Content selection from URL?



Strangeplant
09-20-2006, 04:25 PM
I'm using the Dynamic Ajax Content script at http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.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?

ddadmin
09-20-2006, 08:46 PM
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.

blm126
09-20-2006, 08:56 PM
Or do you mean that you link to page that will automatically load a url?

Strangeplant
09-21-2006, 12:48 PM
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:
http://www.somesite.com/ajaxpage.html?file=nametoload&title=hotcontentI 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):
<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:
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.

blm126
09-21-2006, 08:40 PM
function getQueryValue(name) {
var match = (new RegExp('[?&;]' + name + '=([^&;#]*)')).exec(document.URL);
return match ? unescape(match[1]) : null;
}

The above function from this thread (http://www.dynamicdrive.com/forums/showthread.php?t=11883&page=3&highlight=split) is much better(I think) So, add this to the javascript


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


http://yourdomain.com/page.html?page=sompage.html

Strangeplant
09-22-2006, 02:26 PM
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:
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.

blm126
09-22-2006, 08:43 PM
no problem