Results 1 to 3 of 3

Thread: AJAX and Javascript: two functions from one link?

  1. #1
    Join Date
    Oct 2008
    Posts
    21
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default AJAX and Javascript: two functions from one link?

    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):
    Code:
     <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):
    Code:
     <a href="#" id="toggle">Click here to toggle panel</a>
    The second script is an ajax loader (HEAD):
    Code:
     <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):
    Code:
     <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
    Code:
    <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

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Do you have a live demonstration of this?
    - Josh

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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):

    Code:
    <!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>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •