Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending a link with Jquery

I'm trying to add a link to my page depending on what page you're already on. I'm using Squarespace to build this site, so the easiest way for me to do this would be with Javascript or Jquery.

I think there is something wrong with this syntax that i'm missing. I already tried to break out of the quotes with a \, but that wasn't working. If I'm just outputting text, it works fine, but it seems to break when I try and make it work with a link.

 if(loc == pageOne) {
$("#pages").append("<div> <a href=\"http://design.optimus.com/projects?currentPage=2\">Next Page</a> </div>")
    }else{
  if(loc == pageOneB){
    $("#pages").append("<div> <a href=\"http://design.optimus.com/projects?currentPage=2\">Next Page</a> </div>")
  }else{
    if(loc == pageTwo){
     $("#pages").append("<div> <a href=\"http://design.optimus.com/projects\">Previous Page</a></div> ")
  }
 }
}

Edit: I just checked, and the links seem to be working in Chrome, but not Firefox.

Another Edit: So the link is working in Safari too. I guess the issue now is why not in Firefox? Is there something about this method that Firefox doesn't support?

like image 221
patricko Avatar asked Feb 23 '12 20:02

patricko


1 Answers

Your code seems to work fine however you can use else if statements instead of nested if statements:

if(loc == pageOne) {
    $("#pages").append("<div> <a href=\"http://design.optimus.com/projects?currentPage=2\">Next Page</a> </div>")
} else if (loc == pageOneB){
    $("#pages").append("<div> <a href=\"http://design.optimus.com/projects?currentPage=2\">Next Page</a> </div>")
} else if (loc == pageTwo){
    $("#pages").append("<div> <a href=\"http://design.optimus.com/projects\">Previous Page</a></div> ")
}​

Here is a demo: http://jsfiddle.net/Rba6w/

like image 114
Jasper Avatar answered Sep 27 '22 22:09

Jasper