SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why href does not work with this function

    Hi all, how are you?

    I've a problem with this ajax function, the url doesn't actualize. I want to see the hash in url, but with the function it's not possible. Why?

    The code I use in php is:

    Code:
    <br><a href="#nosotros" onClick="FAjax('nosotros.php' ,'capaContenedora','','get'); return false"><b>nosotros</b></a>
    With this, I'd like to see on my website: http://www.mywebsite.com/#nosotros

    And the function is:

    Code:
    function FAjax (url,capa,valores,metodo)
    {
       var ajax=creaAjax();
       var capaContenedora = document.getElementById(capa);
       			
    		ajax.open ('GET', url, true);
    		ajax.onreadystatechange = function() {
    			 if (ajax.readyState==1) {
    					 capaContenedora.innerHTML="<img src=imagenes/cargando.gif>";
    			 }
    			 else if (ajax.readyState==4){
    				if(ajax.status==200){ 
    					 document.getElementById(capa).innerHTML=ajax.responseText; 
    				}
    				else if(ajax.status==404)
    					 {
    	
    						 capaContenedora.innerHTML = "La direccion existe";
    					 }
    					 else
    					 {
    						 capaContenedora.innerHTML = "Error: ".ajax.status;
    					 }
    			}
    		}
    		ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    		ajax.send(null);
    		return
    }
    TY so much!

  2. #2
    Follow: @AlexDawsonUK silver trophybronze trophy AlexDawson's Avatar
    Join Date
    Feb 2009
    Location
    England, UK
    Posts
    8,111
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Return false disables the navigation to a particular link in order to run the script, thereby you're going to encounter the issue of no hash in the address bar, a better method would be to examine the hash using "var hash = location.hash.substr(1);" (in your JavaScript file) to get the hash value when it changes and then do whatever you need as and when the hash matches the value you want. Much less obtrusive and it should do the job perfectly!

  3. #3
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OMG, now I understand. Thank you so much Alex.

    I'm following you now in twitter! @carlossaez

    TY so much.

  4. #4
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi all another time

    I've a doubt now collecting the hash. I use in function hash.js this code:

    Code:
    $(document).ready(function(){
        //obtain the hash
       var jash = window.location.hash;
        // if it is valid
       if(jash.length > 1){
          // if it exists, we look for the title attribute
          var href = $("a[@href="+jash+"]").attr("title");
          // we load the page
          $("#capaContenedora").load(href);
       }
    
        
    });

    This line is not working I think, because I'm placing some alerts and the variable href doesn't exist: var href = $("a[@href="+jash"]").attr("title");

    Is this correct?? -> $("a[@href="+jash+"]") <-

    Thank you very much in advance.

  5. #5
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, problem solved. If I delete the @ it works correctly!

    TY!

    Quote Originally Posted by sitoatic View Post
    Hi all another time

    I've a doubt now collecting the hash. I use in function hash.js this code:

    Code:
    $(document).ready(function(){
        //obtain the hash
       var jash = window.location.hash;
        // if it is valid
       if(jash.length > 1){
          // if it exists, we look for the title attribute
          var href = $("a[@href="+jash+"]").attr("title");
          // we load the page
          $("#capaContenedora").load(href);
       }
    
        
    });

    This line is not working I think, because I'm placing some alerts and the variable href doesn't exist: var href = $("a[@href="+jash"]").attr("title");

    Is this correct?? -> $("a[@href="+jash+"]") <-

    Thank you very much in advance.

  6. #6
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Take a look here
    http://www.learningjquery.com/2007/1...ame-page-links
    to retrieve the hash.

    Bye.

  7. #7
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whisher View Post
    Hi,
    Take a look here
    http://www.learningjquery.com/2007/1...ame-page-links
    to retrieve the hash.

    Bye.
    Hi whisher, TY a lot, all problems about this solved!

    Now I 've a nother one. The hash I'm passing is like this:

    #archive.php?var1=res1&var2=res2

    I do all splits correctly, and I obtain all variables correctly renamed as I want, including the archive where we need to call 'archive.php' with:

    $("#capaContenedora").load(archivo);

    But, in this archive.php that I normally collect all variables with $_GET is not working collecting the variables from hash. Is the method I'm using correct?

    Thank you in advance, and sorry by my english not at all right

  8. #8
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not too sure what you are playing around
    but try to send data to the php script
    Code JavaScript:
    $("#objectID").load("test.php", { 'var1':res1 } );

    You can't retrieve hash server side.

  9. #9
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whisher View Post
    Not too sure what you are playing around
    but try to send data to the php script
    Code JavaScript:
    $("#objectID").load("test.php", { 'var1':res1 } );

    You can't retrieve hash server side.
    TY so much whisher, I think that it's something similar I need... I'll continue, and when I have the correct code I'll put here!

    TY!

  10. #10
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!

    I've this part finished! I divided the hash:

    Code:
    var jash = window.location.hash;
    var gash = jash.substring(1);
    	
    splitter = gash.split('?');
    archivo = splitter[0];
    restoant = splitter[1];
    And then, when I wanna load the page:

    Code:
    $("#capaContenedora").load(archivo, restoant);

    If I don't split the archive part and all attributes or variables it doesn't work correctly. Now it works.

    Thanks all by your help!


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
  •