PDA

View Full Version : AJAX and Javascript: two functions from one link?



mastubbs
11-23-2009, 01:56 AM
Hi all!

I’m trying to do something a little tricky. Can anyone help:

I want to combine two functions of two different script with the click of a single link. Ie, call a function using a link that itself calls other functions...

The first script is mootools driven and is a panel that toggles when you click a link.
Script (HEAD):

<script type="text/javascript" src="mootools.svn.js"></script>
<script type="text/javascript">
window.addEvent('domready', function(){
var mySlide = new Fx.Slide('top-panel');

$('toggle').addEvent('click', function(e){
e = new Event(e);
mySlide.toggle();
e.stop();
});
});

</script>
Link to toggle panel (BODY):

<a href="#" id="toggle">Click here to toggle panel</a>
The second script is an ajax loader (HEAD):

<script type="text/javascript">

/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
document.getElementById(containerid).innerHTML='<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="49%" class="LoadingDataLeft"></td><td width="2%" align="center" valign="middle"><img src="loadingdata.gif" alt="LoadingData" width="32" height="32" /></td><td width="49%" class="LoadingDataRight"></td></tr></table>'
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}

</script>
The page called into the div is by (BODY):

<a href="javascript:ajaxpage(‘content_to_call.html', 'area_to_load');">test</a>

<div id="area_to_load"></div>

SO, what I want to do is make a single link that, when clicked, both loads a page into the ‘area_to_load’ div and also toggles the panel. In fact, to make it even more tricky I want lots of links which both load different content into ‘area_to_load’ (depending on the link clicked) and also toggle the panel… if that makes sense?

I have tried
<a href="javascript:ajaxpage(‘content_to_call.html', 'area_to_load');" id="toggle">Click here to toggle panel</a> but did not work :-(

I really cant figure out how to do it so I would be really greatful for any help anyone can give.

Thanks

Matt

JShor
11-23-2009, 01:58 AM
Do you have a live demonstration of this?

jscheuer1
11-23-2009, 03:50 AM
That doesn't make a lot of sense to me simply because, and it can be done, when it's done, say you click on one link to load one thing into the 'area_to_load' division. When you do that, it also toggles the 'top-panel' division. Click on another one, and it again toggles the 'top-panel' division. So, if you are following, there is no logical sequence to the toggle of 'top-panel'. If it's open, it closes. If it's closed, it opens. The only controlling factor is whether or not it was open or closed when a link that toggles it is clicked.

But, if you are willing to live with that (I guess it could be OK, depending upon the layout, and more importantly, the content):


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1/mootools-yui-compressed.js"></script>
<script type="text/javascript">
window.addEvent('domready', function(){
var mySlide = new Fx.Slide('top-panel');

$$('.toggle').addEvent('click', function(e){
e = new Event(e);
mySlide.toggle();
ajaxpage(this.href, this.rel);
e.stop();
});
});
</script>
<script type="text/javascript">

/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest && (typeof ActiveXObject === 'undefined' || /^http/.test(location.href))) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
document.getElementById(containerid).innerHTML='<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="49%" class="LoadingDataLeft"></td><td width="2%" align="center" valign="middle"><img src="loadingdata.gif" alt="LoadingData" width="32" height="32" /></td><td width="49%" class="LoadingDataRight"></td></tr></table>'
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}

</script>
</head>
<body>
<div id="top-panel">
Hey
</div>
<div>
<a href="content_to_call.htm" rel="area_to_load" class="toggle">test</a><br>
<a href="content_to_call2.htm" rel="area_to_load" class="toggle">test2</a><br>

<div id="area_to_load"></div>
</div>
</body>
</html>